Java语言程序设计基础篇原书第十版第二章编程练习题答案

时间:2021-04-15 21:49:48

程序如有问题,及时联系博主哦~博主会贴出所有带有星号的题目,以及一些典型的例子,希望能够帮助到同学们,也希望同学们都学好java语言。

2.1将摄氏温度转换为华氏温度

package nameyu;

import java.util.Scanner;

public class Test {

/**
* @param args
*/

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.print("Enter a degree in celsius:");
double Celsiustemperature=input.nextDouble();
double Fahrenheit=(9.0/5)*Celsiustemperature+32;
System.out.println(Celsiustemperature+"Cellsius is "+Fahrenheit+" Fahrenheit");

}

}

2.5财务应用小程序:计算小费

package nameyu;

import java.util.Scanner;

public class Test {

/**
* @param args
*/

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner (System.in);
System.out.print("Enter the subtotal and a gratuity rate:");
double subtotal=input.nextDouble();
double gratuityrate=input.nextDouble();
double gratuity=subtotal*gratuityrate*0.01;
double total=subtotal+gratuity;
System.out.println("The gratuity is $"+gratuity+" and total is $"+total);
}

}

2.6求一个整数各位数的和

package nameyu;

import java.util.Scanner;

public class Test {

/**
* @param args
*/

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.print("Enter a number 0 and 1000 :");
int number =input.nextInt();
int bit=number%10;
number=number/10;
int ten=number%10;
number=number/10;
int kil=number%10;
System.out.println("The sum of the digits is "+(bit+ten+kil));

}


}
这是一种没有循环的做法,比较死板,但是很容易理解,下面介绍一种好的方法

续上:

package nameyu;

import java.util.Scanner;

public class Test {

/**
* @param args
*/

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.print("Enter a number 0 and 1000 :");
int number =input.nextInt();
int sum=0;
while(number/10!=0){
sum=sum+number%10;
number=number/10;
}
sum=sum+number;
System.out.println("The sum of the digtits is :"+sum);
}


}

2.7求出年数

package nameyu;

import java.util.Scanner;

public class Test {

/**
* @param args
*/

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.print("Enter the number of minutes :");
int number =input.nextInt();
int years=number/(60*24*365);
// int days=number%(60*24*365);
int days=number/(60*24)-years*365;
System.out.println(days);

}


}

2.8当前时间

package nameyu;

import java.util.Scanner;

public class Test {

/**
* @param args
*/

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.print("Enter the time zone offset to GMT :");
int GMT =input.nextInt();
long totalMilliseconds=System.currentTimeMillis();
long totalSeconds=totalMilliseconds/1000;
long currentSecond=totalSeconds%60;
long totalMinutes=totalSeconds/60;
long currentMinutes=totalSeconds%60;
long totalHours=totalMinutes/60;
long currentHours=totalHours%24;
System.out.println("Current time is"+(currentHours+GMT)+":"+currentMinutes+":"+currentSecond);

}

2.13(财务应用程序)复利值

package nameyu;

import java.util.Scanner;

public class Test {

/**
* @param args
*/

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.print("Enter the monthly saving amount and mouth:");
double save=input.nextDouble();
int mouths=input.nextInt();
double yearsrate=0.05;
double mouthrate=(yearsrate/12);
double total=0;
for(int i=1;i<=mouths;i++){

total=(total+save)*(1+mouthrate);

}

System.out.println((int)(total*100)/100.0);




}


}

2.14医疗应用程序:计算BMI

package nameyu;

import java.util.Scanner;

public class Test {

/**
* @param args
*/

public static void main(String[] args) {
// TODO Auto-generated method stub
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();
double rice=height*0.0254;
double kg=weight*0.45359237;
double BMI=kg/(rice*rice);
System.out.println("BMI is"+BMI);
}


}

2.17科学,风寒温度

package nameyu;

import java.util.Scanner;

public class Test {

/**
* @param args
*/

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.print("Enter the temperature in Fahrenheit between -58°F and 41°F:");
double Fahrenheit=input.nextDouble();
System.out.print("Enter the wind speed (x>=2) in miles per hour:");
double speed=input.nextDouble();
if(speed>=2&&Fahrenheit>-58&Fahrenheit<41){
double twc=35.74+0.6215*Fahrenheit-35.75*Math.pow(speed, 0.16)+0.4275*Fahrenheit*Math.pow(speed, 0.16);
System.out.println(twc);
}
else
System.out.println("error");
}


}

2.18打印表格

package nameyu;
//import java.util.Scanner;

public class Test {

/**
* @param args
*/

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.printf("%-5s%-5s%-5s%n", "a","b","pow(a,b)");
/*这个地方可能有的同学会有问题,使用printf或者format输出的时候,有的机器需要在window里面配置如果有任何问题可以邮件联系博主,有时间我会把教程发在博客中。*/
for (int i=1;i<6;i++){
int s=(int)Math.pow(i, i+1);
System.out.printf("%-5d%-5d%-5d%n", i,i+1,s);
}
}
}

2.19几何:三角形的面积

package nameyu;
import java.util.Scanner;

public class Test {

/**
* @param args
*/

public static void main(String[] args) {
// TODO Auto-generated method stub
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 s1=Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
double s2=Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1));
double s3=Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
double s=(s1+s2+s3)/2;
double area=Math.sqrt(s*(s-s1)*(s-s2)*(s-s3));
System.out.println("The area of the triangle is "+(int)(area*10)/10.0);
}
}

2.20财务应用程序:计算利息

package nameyu;
import java.util.Scanner;

public class Test {

/**
* @param args
*/

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("Enter balance and interest rate(e.g.,3 for 3%):");
double money=input.nextDouble();
double rate=input.nextDouble();
double interest=money*(rate/1200);
System.out.println("The interest is "+(int)(interest*100000)/100000.0);


}
}

2.22财务应用:货币单位

package nameyu;
import java.util.Scanner;

public class Test {

/**
* @param args
*/

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.print("Enter an amount in int,for example 1156($11.56):");
int amount=input.nextInt();
int dollar=amount/100;
int cent=amount%100;
System.out.println("This amout ="+dollar+"."+cent);


}
}

这一章的课后编程题大概也就是这些了,重点的都已经给出了代码,并且亲自测试了的,有出错或者什么问题的都可以联系博主。编写代码不容易,兄弟们帮忙顶顶