以下答案都是自己做的答案,可能会有错误的地方。如果核对的时候有发现有误的,请指导我改正一下!!
2.1(将摄氏温度转换为华氏温度)
package demo2;
import java.util.Scanner;
public class demo01 {
public static void main(String[] args){
System.out.println("Enter a degree in Celsius:");
Scanner input=new Scanner(System.in);
double temperature=input.nextDouble();
double Fahrenheit=(9.0/5)*temperature+32;
System.out.println(temperature+" Celsius is "+Fahrenheit+" Fahrenheit");
}
}
2.2(计算圆柱体的体积)
package demo2;
import java.util.Scanner;
public class demo02 {
public static void main(String[] args){
System.out.println("Enter the radius and length of a cylinder:");
Scanner input=new Scanner(System.in);
double radius=input.nextDouble();
double height=input.nextDouble();
System.out.println("The area is "+radius*radius*Math.PI+"\n"
+ "The volumme is "+radius*radius*Math.PI*height);
}
}
2.3(将英尺转换为米)
package demo2;
import java.util.Scanner;
public class demo03 {
public static void main(String[] args){
System.out.println("Enter a value for feet:");
Scanner input=new Scanner(System.in);
double feet=input.nextDouble();
System.out.println(feet+" feet is "+feet*0.305+" meters");
}
}
2.4(将磅转换为千克)
package demo2;}
2.5(财务应用程序:计算小费)
package demo2;
import java.util.Scanner;
public class demo05 {
public static void main(String[] args){
System.out.println("Enter the subtotal and a gratuity rate");
Scanner input=new Scanner(System.in);
double money=input.nextDouble();
double rate=input.nextDouble();
System.out.println("The gratuity is "+money*rate/100+" and total is "+money*(1+rate/100));
}
}
2.6(求一个整数各位数的和)
package demo2;
import java.util.Scanner;
public class demo06 {
public static void main(String[] args){
System.out.println("Enter a number between 0 and 1000:");
Scanner input=new Scanner(System.in);
int number=input.nextInt();
int sum=0;
while(number!=0){
int temp=number%10;
sum+=temp;
number/=10;
}
System.out.println("The sum of the digits is "+sum);
}
}
2.7(求出年数)
package demo2;
import java.util.Scanner;
public class demo07 {
public static void main(String[] args){
System.out.println("Enter the number of minutes:");
Scanner input=new Scanner(System.in);
int totolMinutes=input.nextInt();
//计算总天数
int totolDay=totolMinutes/(60*24);
//计算总年数
int totalYear=totolMinutes/(60*24*365);
//计算当前的天数
int currentDay=totolDay%(365);
System.out.println(totolMinutes+" minutes is approximatelly "+totalYear+" years and "+
currentDay+" days");
}
}
2.8(当前时间)
package demo2;
import java.util.Scanner;
public class demo08 {
public static void main(String[] args){
System.out.println("Enter the time zone offset to GMT");
Scanner input=new Scanner(System.in);
int gmt=input.nextInt();
//得到总毫秒
long totalMilliseconds=System.currentTimeMillis();
//计算总秒数
long totolSeconds=totalMilliseconds/1000;
//计算当前的第几秒,商是总分钟数
long currentSecond=totolSeconds%60;
//计算总分钟数
long totalMinutes=totolSeconds/60;
//计算当前第几分钟,商是总小时数
long currentMinute=totalMinutes%60;
//计算总小时数
long totolHours=totalMinutes/60;
//计算当前第几个小时
long currentHour=totolHours%24;
System.out.println("Current time is "+(currentHour+gmt)+
":"+currentMinute+":"+currentSecond+" GMT");
}
}
2.9(物理:加速度)
package demo2;
import java.util.Scanner;
public class demo09 {
public static void main(String[] args){
System.out.println("Enter v0,v1, and t:");
Scanner input=new Scanner(System.in);
double v0=input.nextDouble();
double v1=input.nextDouble();
double t=input.nextDouble();
System.out.println("The average acceleration is: "+(v1-v0)/t);
}
}
2.10(科学:计算能量)
package demo2;
import java.util.Scanner;
public class demo10 {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("Enter the amount of water in kilograms:");
double kilograms=input.nextDouble();
System.out.println("Enter the initial temperature:");
double initialTemperature=input.nextDouble();
System.out.println("Enter the final temperature:");
double finalTemperature=input.nextDouble();
System.out.println("The energy needed is :"
+kilograms*(finalTemperature-initialTemperature)*4184);
}
}
2.11(人口统计)
package demo2;
import java.util.Scanner;
public class demo11 {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("Enter the number of yeras:");
int year=input.nextInt();
int currentPopulation=312032486;
//计算总秒数
final int TOTOL_SECONDS=year*365*24*60*60;
//出生人口
int birthPopulation=TOTOL_SECONDS/7;
//死亡人口
int deathPopulation=TOTOL_SECONDS/13;
//移民人口
int migratePopulation=TOTOL_SECONDS/45;
//计算总人口
int TotolPopulation=(currentPopulation+(birthPopulation+migratePopulation-deathPopulation));
System.out.println("The population in "+year+" year is "+TotolPopulation);
}
}
2.12(物理:求出跑到长度)
package demo2;
import java.util.Scanner;
public class demo12 {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("Enter speed and acceleration:");
double v=input.nextDouble();
double a=input.nextDouble();
System.out.println("The minimun runway length for this airplane is "
+Math.pow(v, 2)/(2*a));
}
}
2.13(财务应用程序:复利值)
package demo2;
import java.util.Scanner;
public class demo13 {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("Enter the monthly saving amount:");
final double monthlyRate=0.00417;
double money=input.nextDouble();
//每个月定时教的钱
final double MONEY=money;
money=money*(1+monthlyRate);
//计算第2个月到第6个月的账户值
for(int i=2;i<=6;i++){
money=(money+MONEY)*(1+monthlyRate);
}
System.out.println("After the sixth month, the account value is $"+money);
}
}
2.14(医疗应用程序:计算BMI)
package demo2;
import java.util.Scanner;
public class demo14 {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("Enter weight in pounds:");
double weight=input.nextDouble();
System.out.println("Enter height in inches:");
double height=input.nextDouble();
System.out.println("BMI is "+weight*0.45359237/Math.pow(height*0.0254, 2));
}
}
2.15(几何:两点间距离)
package demo2;
import java.util.Scanner;
public class demo15 {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("Enter x1 and y1:");
double x1=input.nextDouble();
double y1=input.nextDouble();
System.out.println("Enter x2 and y2:");
double x2=input.nextDouble();
double y2=input.nextDouble();
System.out.println("The distance between the two points is "
+Math.sqrt(Math.pow(x2-x1, 2)+Math.pow(y2-y1, 2)));
}
}
2.16(几何:六边形面积)
package demo2;
import java.util.Scanner;
public class demo16 {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("Enter the side:");
double size=input.nextDouble();
System.out.println("The area of the hexaon is "+3*Math.sqrt(3)*Math.pow(size, 2)/2);
}
}
2.17(科学:风寒温度)
package demo2;
import java.util.Scanner;
public class demo17 {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("Enter the temperature in Fahrenheit between -58 F and 41 F:");
double fahrenheit=input.nextDouble();
System.out.println("Enter the wind speed (>=2) in miles per hour:");
double speed=input.nextDouble();
System.out.println("The wind chill index is "
+(35.74+0.6215*fahrenheit-35.75*Math.pow(speed, 0.16)
+0.4275*fahrenheit*Math.pow(speed, 0.16)));
}
}
2.18(打印表格)
package demo2;
import java.util.Scanner;
public class demo18 {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("a b pow(a,b)");
while(input.hasNext()){
int a=input.nextInt();
int b=input.nextInt();
System.out.println(a+" "+b+" "+(int)Math.pow(a, b));
}
}
}
2.19(几何:三角形的面积)
package demo2;
import java.util.Scanner;
public class demo19 {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("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 a=Math.sqrt(Math.pow(x1-x2, 2)+Math.pow(y1-y2, 2));
double b=Math.sqrt(Math.pow(x1-x3, 2)+Math.pow(y1-y3, 2));
double c=Math.sqrt(Math.pow(x2-x3, 2)+Math.pow(y2-y3, 2));
double s=(a+b+c)/2;
double area=Math.sqrt(s*(s-a)*(s-b)*(s-c));
System.out.println("The area of the triangle is "+area);
}
}
2.20(财务应用程序:计算利息)
package demo2;
import java.util.Scanner;
public class demo20 {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("Enter balance and interest rate:");
double balance =input.nextDouble();
double rate=input.nextDouble();
double interest=balance*(rate/1200);
System.out.println("The interest is "+interest);
}
}
2.21(财务应用:计算未来投资值)
package demo2;
import java.util.Scanner;
public class demo21 {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("Enter investment amount:");
double investmentAmount=input.nextDouble();
System.out.println("Enter annual interest rate in percemtage:");
double rate=input.nextDouble();
System.out.println("Enter number of years:");
int year=input.nextInt();
double value=investmentAmount*Math.pow((1+rate/1200), year*12);
System.out.println("Accumulated value is :"+value);
}
}
2.22(财务应用:货币单位)
package demo2;
import java.util.Scanner;
public class demo22 {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("Enter an amount in int, for example 1156:");
int amount=input.nextInt();
//计算多少张美元
int numberOfOneDollars=amount/100;
amount=amount%100;
//计算多少张二角五分
int numberOfOneQuarters=amount/25;
amount=amount%25;
//计算多少张一角
int numberOfOneDime=amount/10;
amount=amount%10;
//计算多少张五分
int numberOfOneNickel=amount/5;
amount=amount%5;
//计算多少张一分
int numberOfOnePenny=amount/1;
System.out.println("Your amount "+amount+" consists of");
System.out.println(" "+numberOfOneDollars+" dollars");
System.out.println(" "+numberOfOneQuarters+" quarter");
System.out.println(" "+numberOfOneDime+" dime");
System.out.println(" "+numberOfOneNickel+" nickel");
System.out.println(" "+numberOfOnePenny+" penny");
}
}
2.23(驾驭费用)
package demo2;
import java.util.Scanner;
public class demo23 {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("Enter the driving distance:");
double distance=input.nextDouble();
System.out.println("Enter miles per gallon:");
double miles=input.nextDouble();
System.out.println("Enter price per align gallon:");
double price=input.nextDouble();
double cost=(distance/miles)*3.55;
System.out.println("The cost of driving is :"+cost);
}
}