2.1(将摄氏度转换为华氏温度)编写程序,从控制台读入double型的摄氏温度,然后将其转换为华氏温度,并且显示结果。转换公式如下所示:

华氏温度 = (9 / 5)* 摄氏温度 + 32

提示:在Java中,9 / 5的结果是1,但是9.0 / 5的结果是1.8

下面是一个运行示例:

Enter a degree in Celsius : 43

43 Celsius is 109.4 Fahrenheit

 

解:

import java.util.Scanner;

public class Test1{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

System.out.print(“Enter a degree in Celsius: “);

double celsius = input.nextDouble();

double fahrenheit = (9.0 / 5) * celsius + 32;

System.out.println(celsius + ” Celsius is ” + fahrenheit +

” Fahrenheit”);

}

}

 

 

2.2(计算圆柱体的体积)编写程序,读入圆柱体的半径和高,并使用下列公式计算圆柱的体积:

面积 = 半径 * 半径 * p

体积 = 面积 * 高

下面是一个运行示例:

Enter the radius and length of a cylinder: 5.5  12

The area is 95.0331

The volume is 1140.4

 

解:

import java.util.Scanner;

public class Test2{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

System.out.print(“Enter the radius and length of a cylinder: “);

double radius = input.nextDouble();

double length = input.nextDouble();

double p = 3.14159;

double area = radius * radius * p;

double volume = area * length;

System.out.println(“The area is ” + area);

System.out.println(“The volume is ” + volume);

}

}

 

2.3(将英尺转换为米)编写程序,读入英尺数,将其转换为米数并显示结果。一英尺等于0.305米。

下面是运行示例:

Enter a value for feet: 16.5

16.5 feet is 5.0325 meters

 

解:

import java.util.Scanner;

public class Test3{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

System.out.print(“Enter a value for feet: “);

double feet = input.nextDouble();

double meter = 0.305 * feet;

System.out.println(feet + ” feet is ” + meter + ” meters”);

}

}

 

2.4(将磅转换为千克)编写程序,将磅数转换为千克数。程序提示用户输入磅数,然后转换成千克并显示结果。一磅等于0.454千克。

下面是一个运行示例:

Enter a number in pounds: 55.5

55.5 pounds is 25.197 kilograms

 

解:

import java.util.Scanner;

public class Test4{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

System.out.print(“Enter a number of pounds: “);

double pound = input.nextDouble();

double kilogram = pound * 0.454;

System.out.println(pound + ” pounds is ” + kilogram +

” kilograms”);

}

}

 

 

2.5(财务应用程序:计算小费)编写一个程序,读入一笔费用与酬金率,计算酬金和总钱数。例如,如果用户输入10作为费用,15%作为酬金率,计算结果显示酬金为$1.5,总费用为$11.5。下面是一个运行示例:

Enter the subtotal and a gratuity rate: 10  15

The gratuity is $1.5 and total is $11.5

 

解:

import java.util.Scanner;

public class Test5{

public static void main(String[] args){

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 / 100.0);

double total = gratuity + subtotal;

System.out.println(“The gratuity is ” + gratuity +

” and total is ” + total);

}

}

 

 

2.6(求一个整数各位数的和)编写程序,读取一个在0和1000之间的整数,并将该整数的各位数字相加,例如:整数是932,各位数字之和为9 + 3 + 2 = 14

提示:利用操作符%分解数字,然后使用操作符 / 去掉分解出来的数字。例如:

932 % 10 = 2,  932 / 10 = 93  下面是一个运行示例:

Enter a number between 0 and 1000: 999

The sum of the digits is 27

 

解:

import java.util.Scanner;

public class Test6{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

System.out.print(“Enter a number between 0 and 1000: “);

int number = input.nextInt();

int num1 = number % 10;

int number1 = number / 10;

int num2 = number1 % 10;

int number2 = number1 /10;

int num3 = number2 % 10;

int sum = num1 + num2 + num3;

System.out.print(“The sum of the digits is ” + sum);

}

}

 

 

2.7(求出年数)编写程序,提示用户输入分钟数(例如十亿)然后显示这些分钟代表多少年和多少天。为了简化问题,假设一年有365天。下面是一个运行示例:

Enter the number of minutes: 1000000000

1000000000 minutes is approximately 1902 years and 214 days

 

解:

import java.util.Scanner;

public class Test7{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

System.out.print(“Enter the number of minutes: “);

int num = input.nextInt();

int yearTime = 365 * 24 * 60;

int dayTime = 24 * 60;

int year = num / yearTime;

int num1 = num % yearTime;

int day = num1 / dayTime;

System.out.println(num + ” minutes is approximately ” +

year + ” and ” + day + ” days”);

}

}

 

 

2.8(当前时间)程序清单2-7给出了显示当前格林威治时间的程序。修改这个程序,提示用户输入相对于GMT的时区偏移量,然后显示在这个特定时区的时间。下面是一个运行示例:

Enter the time zone offset to GMT: -5

The current time is 4:50:34

 

解:

import java.util.Scanner;

public class Test8{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

System.out.print(“Enter the time zone offset to GMT: “);

int zone = input.nextInt();

 

long totalMilliseconds = System.currentTimeMillis();

long totalSeconds = totalMilliseconds / 1000;

long currentSeconds = totalSeconds % 60;

long totalMinutes = totalSeconds / 60;

long currentMinutes = totalSeconds % 60;

long totalHours = totalMinutes / 60;

long currentHours = totalHours % 24;

int Hour1 = (int)currentHours + zone;

System.out.println(“The current time is ” + Hour1

+ “:” + currentMinutes + “:” + currentSeconds);

}

}

 

 

2.9(物理:加速度)平均加速度定义为速度的变化量除以这个变化所用的时间,如下式所示:

a = (v1 – v2)/ t

编写程序,提示用户输入以米/秒为单位的其实速度v0,以米/秒为单位的终止速度v1,以及以秒为单位的时间段t,最后显示平均加速度。下面是一个运行示例:

Enter v0, v1, and t: 5.5  50.9  4.5

The average acceleration is 10.0889

 

解:

import java.util.Scanner;

public class Test9{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

System.out.print(“Enter v0, v1, and t: “);

double v0 = input.nextDouble();

double v1 = input.nextDouble();

double t = input.nextDouble();

double acceleration = (v1 – v0) / t;

System.out.println(“The average acceleration is ” +

acceleration);

}

}

 

 

2.10(科学:计算能量)编写程序,计算将水从初始温度加热到最终温度所需的能量。程序应该提示用户输入水的重量(以千克为单位),以及水的初始温度和最终温度。计算能量的公式是:

Q = M * (最终温度 – 初始温度) * 4184

这里的M是以千克为单位的水的重量,温度以摄氏度为单位,儿能量Q以焦耳为单位。下面是一个运行示例:

Enter the amount of water in kilograms: 55.5

Enter the initial temperature: 3.5

Enter the final temperature: 10.5

The energy needed is 1625484.0

 

解:

import java.util.Scanner;

 

public class Test10{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

System.out.print(“Enter the amount of water in kilograms: “);

double amount = input.nextDouble();

System.out.print(“Enter the initial temperature: “);

double t = input.nextDouble();

System.out.print(“Enter the final temperature: “);

double t1 = input.nextDouble();

double energy = amount * (t1 – t) * 4184;

System.out.println(“The energy needed is ” + energy);

}

}

 

 

2.11(人口统计)重写编程练习题1.11,提示用户输入年数,然后显示这个年数之后的人口值。将编程练习题1.11中的提示用于这个程序。人口数应该类型转换为一个整数。下面是一个运行示例:

Enter the number of years: 5

The population in 5 years is 325932970

 

解:

import java.util.Scanner;

 

public class Test11{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

System.out.print(“Enter the number of years: “);

int numberOfYear = input.nextInt();

double population = 312032486 + numberOfYear*(365*24*60*60/7.0 –

365*24*60*60/13.0 + 365*24*60*60/45.0);

System.out.println(“The population in ” + numberOfYear +

” years is ” + (int)population);

}

}

 

 

2.12(物理:求出跑到长度)假设一个飞机的加速度是a而起飞的速度是v,那么可以使用下面的公式计算出飞机起飞所需的最短跑到长度:

跑道长度 =  v2 / 2a

编写程序,提示用户输入以米/秒(m/s)为单位的速度v和以米/秒的平方(m/s2)为单位的加速度a,然后显示最短跑道长度。下面是一个运行示例:

Enter speed and acceleration: 60  3.5

The minimum runway length for this airplane is 514.286

 

解:

import java.util.Scanner;

 

public class Test12{

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.println(“The minimum runway length for this airplane is: ”

+ (int)length + ” meters”);

}

}

 

 

2.13(财务应用程序:复利值)假设你每月向银行账户存100美元,年利率为5%,那么每月利率是0.05/12 = 0.00417 。第一个月之后,账户上的值就变成:

 

100 * (1 + 0.00417) = 100.417

第二个月之后,账户上的值就变成:

(100 + 100.417) * (1 + 0.00417) = 201.252

第三个月之后,账户上的值就变成:

(100 + 201.252)*(1 + 0.00417) = 302.507

以此类推。

编写程序显示六个月后账户上的钱数。(在编程练习题5.30中,你将使用循环来简化这里的代码,并能显示任何一个月之后的账户值。)

Enter the monthly saving amount: 100

After the sixth month, the account value is $608.81

 

解:

import java.util.Scanner;

 

public class Test13{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

System.out.print(“Enter the monthly saving amount: “);

double amount = input.nextDouble();

double currentValue = amount;

 

currentValue = currentValue * (1 + 0.00417);

System.out.println(“After the first month,the account value is ”

+ currentValue);

currentValue = (currentValue + amount) * (1 + 0.00417);

System.out.println(“After the sencond month,the account value is ”

+ currentValue);

currentValue = (currentValue + amount) * (1 + 0.00417);

System.out.println(“After the third month,the account value is ”

+ currentValue);

currentValue = (currentValue + amount) * (1 + 0.05 / 12);

currentValue = (currentValue + amount) * (1 + 0.05 / 12);

currentValue = (currentValue + amount) * (1 + 0.05 / 12);

System.out.println(“After the sixth month, the account value is ”

+ currentValue);

}

}

 

 

2.14(医疗应用程序:计算BMI)身体质量指数(BMI)是对体重的健康测量。他的值可以通过将体重(以公斤为单位)除以身高(以米为单位)的平方值得到。编写程序,提示用户输入体重(以磅为单位)以及身高(以英寸为单位),然后显示BMI。注意:一磅是0.45359237公斤,一英寸是0.0254米。下面是一个运行示例:

Enter weight in pounds: 95.5

Enter height in inches: 50

BMI is 26.8573

 

解:

import java.util.Scanner;

 

public class Test14{

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();

double BMI = weight * 0.45359237 / ((height * 0.0254) * (height * 0.0254));

System.out.println(“BMI is ” + BMI);

}

}

 

 

2.15(几何:两点间距离)编写程序,提示用户输入两个点(x1, y1)和(x2, y2),然后显示两点间的距离。计算两点间距离的公式是

注意:可以使用Math.pow(a, 0.5)来计算。下面是一个运行示例:

Enter x1 and y1: 1.5  -3.4

Enter x2 and y2: 4  5

The distance between the two points is 8.764131445842194

 

解:

import java.util.Scanner;

 

public class Test15{

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 b = x2 – x1;

double c = y2 – y1;

double a = Math.pow(b, 2) + Math.pow(c, 2);

double distance = Math.pow(a, 0.5);

System.out.println(“The distance between the two points is: ”

+ distance);

}

}

 

 

2.16(几何:六边形面积)编写程序,提示用户输入六边形的边长,然后显示他的面积。计算六边形面积的公式是:

面积 =

这里的s就是边长,下面是一个运行示例:

Enter the side: 5.5

The area of the hexagon is 78.5918

 

解:

import java.util.Scanner;

 

public class Test16{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

System.out.print(“Enter the side: “);

double s = input.nextDouble();

double area = 3 * Math.pow(3, 0.5) / 2 * Math.pow(s, 2);

System.out.println(“The area of the hexagon is ”

+ area);

}

}

 

 

2.17(科学:风寒温度)外面到底有多冷?只有温度是不足以提供答案的,包括风速、相对湿度以及阳光等其他的因素在确定室外是否寒冷方面都起了很重要的作用。2001年,国家气象服务(NWS)利用温度和风速计算新的风寒温度,来衡量寒冷程度。计算公式如下所示:

这里的是室外温度,以华氏摄氏度为单位,而v是速度,以每小时英里数为单位。是风寒温度。该公式不适用于风速低于2mph,或温度在-58以下或41以上的情况。

编写程序,提示用户输入在-58和41之间的度数,同时大于或等于2的风速,然后显示风寒温度。使用Math.pow(a, b)来计算下面是一个运行示例:

Enter the temperature in Fahrenheit between -58 and 41:5.3

Enter the wind speed(>=2) in miles per hour: 6

The wind chill index is -5.56707

 

解:

import java.util.Scanner;

 

public class Test17{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

System.out.print(“Enter the temperature in Fahrenheit between -58`F and 41`F: “);

double t = input.nextDouble();

System.out.print(“Enter the wind speed (>=2) in miles per hour: “);

double v = input.nextDouble();

double twc = 35.74 + 0.6215 * t – 35.75 * Math.pow(v, 0.16) + 0.4275 * t * Math.pow(v, 0.16);

System.out.println(“The wind chill index is ” + twc);

}

}

 

 

2.18(打印表格)编写程序,显示下面的表格。将浮点数值类型转换为整数。

 

a      b      pow(a,b)

1      2      1

2      3      8

3      4      81

4      5      1024

5      6      15625

 

解:

public class Test18{

public static void main(String[] args){

System.out.println(“a      b      pow(a,b)”);

System.out.println(“1      2      1”);

System.out.println(“2      3      8”);

System.out.println(“3      4      81”);

System.out.println(“4      5      1024”);

System.out.println(“5      6      15625”);

}

}

 

 

2.19(几何:三角形的面积)编写程序,提示用户输入三角形的三个点(x1,y1)、(x2,y2)和(x3,y3),然后显示它的面积。计算三角形面积的公式是:

S =(边1 + 边2 + 边3)/ 2

面积 =

下面是一个运行示例:

Ebter three points for a triangle: 1.5  -3.4  4.6  5  9.5  -3.4

The area of the triangle is 33.6

 

解:

import java.util.Scanner;

 

public class Test19{

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(Math.pow((x2 – x1), 2) + Math.pow((y2 – y1), 2), 0.5);

double side2 = Math.pow(Math.pow((x3 – x2), 2) + Math.pow((y3 – y2), 2), 0.5);

double side3 = Math.pow(Math.pow((x3 – x1), 2) + Math.pow((y3 – y1), 2), 0.5);

double s = (side1 + side2 + side3) / 2;

double area = Math.pow(s*(s – side1) * (s – side2) * (s – side3), 0.5);

System.out.println(“The area of the triangle is ” + area);

}

}

 

 

2.20(财务应用程序:计算利息)如果知道收支月和年利率的百分比,就可以使用下面的公式计算下个月要支付的利息额:

利息额 = 收支余额 * (年利率 / 1200)

编写程序,读取收支余额和年百分比利率,显示两个版本的下月利息。下面是一个运行示例:

Enter balance and interest rate (e.g.,3 for 3%):1000 3.5

The interest is 2.91667

 

解:

import java.util.Scanner;

 

public class Test20{

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 rate = input.nextDouble();

double interest = balance * (rate / 1200.0);

System.out.println(“The interest is ” + interest);

}

}

 

 

2.21(财务应用:计算未来投资值)编写程序,读取投资总额、年利率和年数,然后使用下面的公式显示未来投资金额:

未来投资金额 = 投资总额 *

例如:如果输入的投资金额为1000,年利率为3.25%,年数为1,那么未来投资额为1032.98

下面是一个运行示例:

Enter investment amount: 1000.56

Enter annual interest rate in percentage: 4.25

Enter number of years: 1

Accumulated value is $1043.92

 

解:

import java.util.Scanner;

 

public class Test21{

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 year = input.nextDouble();

double value = amount * Math.pow((1 + rate / 1200), year * 12);

System.out.println(“Accumulated value is ” + value);

}

}

 

 

2.22(财务应用:货币单位)改写程序清单2-10,解决将double型值转换为int型值时可能会造成精度损失的问题。输入的输入值是一个常数,其最后两位代表的是美分币值。例如:1156就表示的是11美元56美分。

 

解:

import java.util.Scanner;

 

public class Test22{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

System.out.print(“Enter an amount in integer, for example 1156 \nfor 11 dollars and 56 cents: “);

int num = input.nextInt();

int dollar = num / 100;

double pennies1 = num % 100;

int pennies = (int)pennies1;

System.out.println(“Your amount ” + num + ” is ”

+ dollar + ” dollars ” + ” and ” + pennies + ” pennies”);

}

}

 

 

2.23(驾驶费用)编写一个程序提示用户输入驾驶的距离、以及每加仑多少英里的燃油性能,以及每加仑的价格,然后显示旅程的费用。下面是一个运行示例:

Enter the driving distance: 900.5

Enter miles per gallon: 25.5

Enter price per gallon: 3.55

The cost of driving is $125.36

 

解:

import java.util.Scanner;

 

public class Test23{

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 miles = input.nextDouble();

System.out.print(“Enter price per gallon: “);

double price = input.nextDouble();

double cost = price * distance / miles;

System.out.println(“The cost of driving is ” + cost);

}

}

原创文章,转载请注明: 转载自程序员之乱

本文链接地址: Java语言程序设计 第十版课后答案 第二章

分享到: 更多