第二章课后题 Java语言程序设计(基础篇)(第十版)[美]梁勇

Java语言程序设计(基础篇)(第10版)[美]梁勇

第二章课后题P59

//P59 Chapter_2_practise


/*
//T2_1
import java.util.Scanner;

public class Chapter_2_practise {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		
		Scanner input =new Scanner(System.in);

		System.out.println("Enter a degree in Celsius: ");
		
		double celsius=input.nextDouble();
		
		double fahrenheit=(9.0/5)*celsius+32;
		
		System.out.println(celsius+" Celsius is "+fahrenheit+" Fahrenheit");
	}

}
*/

/*
//T2_2
import java.util.Scanner;

public class Chapter_2_practise {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根

		Scanner input =new Scanner(System.in);
		final double PI=3.14159;
		
		System.out.println("Enter the radius and length of a cylender: ");
		
		double radius=input.nextDouble();
		
		double length=input.nextDouble();
		
		double area=PI*radius*radius;
		
		double volume=area*length;
		
		System.out.println("The area is "+area);
		
		System.out.println("The volume is "+volume);
	}

}
*/
/*
//T2_3
import java.util.Scanner;

public class Chapter_2_practise {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		
		Scanner input =new Scanner(System.in);

		System.out.println("Enter a value for feet: ");
		
		double feet=input.nextDouble();
		
		double meter=feet*0.305;
		
		System.out.println(feet+" feet is "+meter+" meters");
	}

}
*/

/*
//T2_4
import java.util.Scanner;

public class Chapter_2_practise {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		
		Scanner input =new Scanner(System.in);

		System.out.println("Enter a number in pounds: ");
		
		double pound=input.nextDouble();
		
		double kilogram=pound*0.454;
		
		System.out.println(pound+" pounds is "+kilogram+" kilograms");
	}

}
*/

/*
//T2_5
import java.util.Scanner;

public class Chapter_2_practise {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		
		Scanner input =new Scanner(System.in);

		System.out.println("Enter the subtotal and a gratuity rate: ");
		
		double subtotal=input.nextDouble();
		
		double gratuityRate=input.nextDouble();
		
		double gratuity=subtotal*gratuityRate/100.0;//100也可以
		
		double total=gratuity+subtotal;
		
		System.out.println("The gratuity is $"+gratuity+" and total is $"+total);
	}

}
*/

/*
//T2_6
import java.util.Scanner;

public class Chapter_2_practise {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		
		Scanner input =new Scanner(System.in);

		System.out.println("Enter a number between 0 and 1000: ");
		
		int number=input.nextInt();
		
		int unitsDigit=number%10;//个位数
		
		int tensDigit=(number/10)%10;//十位数
		
		int hundredsDigit=number/100;//百位数
		
		int sum=unitsDigit+tensDigit+hundredsDigit;
		
		System.out.println("The sum of the digits is "+sum);
	}

}
*/


/*
//T2_7
import java.util.Scanner;

public class Chapter_2_practise {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		
		Scanner input =new Scanner(System.in);

		System.out.println("Enter the number of minutes: ");
		
		long minutes=input.nextLong();
		
		long days=minutes/60/24;
		
		long years=days/365;
		
		long day=days%365;
		
		System.out.println(minutes+" minutes is approximately "+years+" years and "+day+" days");
	}

}
*/

/*
//T2_8
import java.util.Scanner;

public class Chapter_2_practise {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		
		Scanner input =new Scanner(System.in);

		System.out.print("Enter the time zone offset to GMT: ");
		long offset=input.nextLong();
		long totalMilliseconds=System.currentTimeMillis()+offset*60*60*1000;//加时差并转化成毫秒
		long totalSeconds=totalMilliseconds/1000;//总共的秒数
		long currentSecond=totalSeconds%60;//现在的秒
		long totalMinutes=totalSeconds/60;//总共的分钟数
		long currentMinute=totalMinutes%60;//现在的分钟
		long totalHours=totalMinutes/60;//总共的小时数
		long currentHour=totalHours%24;//现在的小时
		    
		System.out.println("Current time is "+currentHour+":"+currentMinute+":"+currentSecond+" GMT");
	}

}
*/

/*
//T2_9
import java.util.Scanner;

public class Chapter_2_practise{
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter v0, v1, and t: ");
		
		double v0,v1,t;
		v0=input.nextDouble();
		v1=input.nextDouble();
		t=input.nextDouble();
		
		double a=(v1-v0)/t;
		
		System.out.print("The average acceleration is "+a);
	} 
}
*/

/*
//T2_10
import java.util.Scanner;

public class Chapter_2_practise{
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		
		
		System.out.print("Enter the amount of water in kilograms: ");
		double waterAmount=input.nextDouble();
		System.out.print("Enter the initial temperature: ");
		double initialTemperature=input.nextDouble();
		System.out.print("Enter the final temperature: ");
		double finalTemperature=input.nextDouble();
		double energy=waterAmount*(finalTemperature-initialTemperature)*4184;
		System.out.print("The energy needed is "+energy);
		
	} 
}
*/
/*
//T1_11
public class Chapter_2_practise{
	public static void main(String[] args) {
	int population=312032486;
	double daita=365*24*3600*(1.0/7-1.0/13+1.0/45);
	int p1=population+(int)daita;
	int p2=population+(int)(2*daita);
	int p3=population+(int)(3*daita);
	int p4=population+(int)(4*daita);
	int p5=population+(int)(5*daita);
	
	System.out.println("The population in 1 year is "+p1);
	System.out.println("The population in 2 years is "+p2);
	System.out.println("The population in 3 years is "+p3);
	System.out.println("The population in 4 years is "+p4);
	System.out.println("The population in 5 years is "+p5);

	} 
}
*/


/*
//T2_11  改写T1_11

import java.util.Scanner;
public class Chapter_2_practise{
	public static void main(String[] args) {
	Scanner input=new Scanner(System.in);
	System.out.print("Enter the number of years: ");
	int year=input.nextInt();
    
	int population=312032486;
	double daita=365*24*3600*(1.0/7-1.0/13+1.0/45);
	
	int newPopulation=population+(int)(year*daita);

	System.out.print("The population in"+year+" years is "+newPopulation);

	} 
}
*/



/*
//T2_12
import java.util.Scanner;

public class Chapter_2_practise{
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter speed and acceleration: ");
		
		double speed=input.nextDouble();
		double acceleration=input.nextDouble();
		double length=speed*speed/2/acceleration;
		
		System.out.print("The minimum runway length for this airplane is "+length);
	} 
}
*/


/*
//T2_13
import java.util.Scanner;

public class Chapter_2_practise{
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		
		System.out.print("Enter the monthly saving amount: ");
		double savingAmount=input.nextDouble();
		
		
		double after1month=savingAmount*(1+0.05/12);
		double after2months=(100+after1month)*(1+0.05/12);
		double after3months=(100+after2months)*(1+0.05/12);
		double after4months=(100+after3months)*(1+0.05/12);
		double after5months=(100+after4months)*(1+0.05/12);
		double after6months=(100+after5months)*(1+0.05/12);
		
		System.out.print("After the sixth month, the account value is $"+after6months);
	} 
}
*/

/*
//T5_30循环简化T2_13
import java.util.Scanner;

public class Chapter_2_practise{
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		
		System.out.print("Enter the monthly saving amount: ");
		double sum=input.nextDouble();
		double newAccountValue=0;
		for(int i=0;i<6;i++)
		{
			newAccountValue=sum*(1+0.05/12);
			sum=100+newAccountValue;
		}
		
		System.out.print("After the sixth month, the account value is $"+newAccountValue);
	} 
}
*/


/*
//T2_14
import java.util.Scanner;

public class Chapter_2_practise{
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		
		System.out.print("Enter weight in pounds: ");
		double weight=input.nextDouble();
		System.out.print("Enter height in inches:");
		double height=input.nextDouble();
		
		weight*=0.45359237;
		height*=0.0254;
		
		double BMI=weight/(height*height);
		
		System.out.print("BMI is "+BMI);
	} 
}
*/


/*
//T2_15
import java.util.Scanner;

public class Chapter_2_practise{
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		
		System.out.print("Enter x1 and y1: ");
		double x1=input.nextDouble();
		double y1=input.nextDouble();
		
		System.out.print("Enter x2 and y2: ");
		double x2=input.nextDouble();
		double y2=input.nextDouble();
		
		double distance=Math.pow((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2),0.5);
		System.out.print("The distance between the two points is "+distance);
	} 
}
*/

/*
//T2_16
import java.util.Scanner;

public class Chapter_2_practise{
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		
		System.out.print("Enter the side: ");
		double side=input.nextDouble();
		
		double area=3*Math.pow(3,0.5)*side*side/2;
		System.out.print("The area of the hexagon is "+area);
	} 
}
*/

/*
//T2_17
import java.util.Scanner;

public class Chapter_2_practise{
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		
		System.out.print("Enter the temperature in Fahrenheit between -58F and 41F: ");
		double temperature=input.nextDouble();
		
		System.out.print("Enter the wind speed (>=2) in miles per hour: ");
		double speed=input.nextDouble();
		
		double index=35.74+0.6215*temperature-35.75*Math.pow(speed,0.16)+0.4275*temperature*Math.pow(speed,0.16);
		System.out.print("The wind chill index is "+index);
	} 
}
*/



/*
//T2_18
public class Chapter_2_practise{
	public static void main(String[] args) {
		
		System.out.println("a     b     pow(a,b)");
		System.out.println(1+"     "+2+"     "+(int)Math.pow(1,2));
		System.out.println(2+"     "+3+"     "+(int)Math.pow(2,3));
		System.out.println(3+"     "+4+"     "+(int)Math.pow(3,4));
		System.out.println(4+"     "+5+"     "+(int)Math.pow(4,5));
		System.out.println(5+"     "+6+"     "+(int)Math.pow(5,6));
	} 
}
*/

/*
//T2_19
import java.util.Scanner;

public class Chapter_2_practise{
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		
		System.out.print("Enter three points for a triangle: ");
		double x1=input.nextDouble();
		double y1=input.nextDouble();
		double x2=input.nextDouble();
		double y2=input.nextDouble();
		double x3=input.nextDouble();
		double y3=input.nextDouble();
		
		double side1=Math.pow((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2),0.5);
		double side2=Math.pow((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3),0.5);
		double side3=Math.pow((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2),0.5);
		
		double side=(side1+side2+side3)/2;
		
		double area=Math.pow(side*(side-side1)*(side-side2)*(side-side3),0.5);
		
		System.out.print("The area of the triangle is "+area);
	} 
}
*/

/*
//T2_20
import java.util.Scanner;

public class Chapter_2_practise{
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		
		System.out.print("Enter balance and interest rate(e.g.,3 for 3%): ");
		double balance=input.nextDouble();
		double interestRate=input.nextDouble();
		double interest=balance*interestRate/1200;
		System.out.print("The interes is "+interest);
	} 
}
*/


/*
//T2_21
import java.util.Scanner;

public class Chapter_2_practise{
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		
		System.out.print("Enter investment amount: ");
		double amount=input.nextDouble();
		
		System.out.print("Enter annual interest rate in percentage: ");
		double rate=input.nextDouble();
		
		System.out.print("Enter number of years: ");
		double years=input.nextDouble();
		
		double accumulatedValue=amount*Math.pow((1+rate/12/100),years*12);
		System.out.print("Accumulated value is $"+accumulatedValue);
	} 
}
*/


/*
//程序清单2-10
import java.util.Scanner;

public class Chapter_2_practise{
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		
		System.out.print("Enter an amount in double, for example, 11.56: ");
		double amount=input.nextDouble();
		
		int remainingAmount=(int)(amount*100);
		
		int numberOfOneDollars=remainingAmount/100;
		remainingAmount%=100;
		
		int numberOfQuarters=remainingAmount/25;
		remainingAmount%=25;
		
		int numberOfDimes=remainingAmount/10;
		remainingAmount%=10;
		
		int numberOfNickels=remainingAmount/5;
		remainingAmount%=5;
		
		int numberOfPennies=remainingAmount;
		
		
		System.out.println("Your amount "+amount+" consists of");
		System.out.println("    "+numberOfOneDollars+" dollars");
		System.out.println("    "+numberOfQuarters+" quarters");
		System.out.println("    "+numberOfDimes+" dimes");
		System.out.println("    "+numberOfNickels+" nickels");
		System.out.println("    "+numberOfPennies+" pennies");
	} 
}
*/

/*
//T2_22
import java.util.Scanner;

public class Chapter_2_practise{
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		
		System.out.print("Enter an amount in int, for example, 1156: ");
		int amount=input.nextInt();
		
		int remainingAmount=amount;
		
		int numberOfOneDollars=remainingAmount/100;
		remainingAmount%=100;
		
		int numberOfQuarters=remainingAmount/25;
		remainingAmount%=25;
		
		int numberOfDimes=remainingAmount/10;
		remainingAmount%=10;
		
		int numberOfNickels=remainingAmount/5;
		remainingAmount%=5;
		
		int numberOfPennies=remainingAmount;
		
		
		System.out.println("Your amount "+amount+" consists of");
		System.out.println("    "+numberOfOneDollars+" dollars");
		System.out.println("    "+numberOfQuarters+" quarters");
		System.out.println("    "+numberOfDimes+" dimes");
		System.out.println("    "+numberOfNickels+" nickels");
		System.out.println("    "+numberOfPennies+" pennies");
	} 
}
*/


//2_23
import java.util.Scanner;

public class Chapter_2_practise{
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		
		System.out.print("Enter the driving distance: ");
		double distance=input.nextDouble();
		
		System.out.print("Enter miles per gallon: ");
		double rate=input.nextDouble();
		
		System.out.print("Enter price per gallon: ");
		double price=input.nextDouble();
		
		double cost=distance/rate*price;
		System.out.print("The cost of driving is $"+cost);
	} 
}

猜你喜欢

转载自blog.csdn.net/qq_41547057/article/details/87864185