200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > Java语言程序设计基础篇(第十版 梁勇著)课后习题答案 - 第二章

Java语言程序设计基础篇(第十版 梁勇著)课后习题答案 - 第二章

时间:2018-08-14 04:19:03

相关推荐

Java语言程序设计基础篇(第十版 梁勇著)课后习题答案 - 第二章

第二章:基本程序设计

复习题

2.1 指出并修改以下代码中的错误:

public class Test {public void main(String[] args) {double i = 50.0;double k = i + 50.0;double j = k + l;System.out.println("j is " + j + " and k is " + k);}}

解:

主方法缺少关键字static;l改为i

正确格式如下:

public class Test {public static void main(String[] args) {double i = 50.0;double k = i + 50.0;double j = k + i;System.out.println("j is " + j + " and k is " + k);}}

2.2 如何编写一条语句,让用户从键盘输入一个双精度值?在执行下面代码的时候,如果你输入5a,将发生什么?

double radius = input.nextDouble();

解:

输入5a的时候,会报错 InputMismatchException

代码如下:

public class Test2_2 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.println("请输入双精度值:");double radius = input.nextDouble();System.out.println(radius);}}

2.3 下面两个import语句之间有什么执行的不同吗?

import java.util.Scanner;import java.util.*;

解:

第一行只会导入until下的Scanner类,第二行会导入Util下的所有的类。

2.4 以下标识符哪些是合法的?哪些是Java的关键字?

miles,Test,a++,–a,4#R,KaTeX parse error: Expected 'EOF', got '#' at position 3: 4,#̲44,apps class,p…组成,其中只有由字母、下划线、$作为开头,且不能由关键字作为标识符;

所以不合法的标识符及其原因是:a++(有+),–a(有 -)、4#R(有#且以数字为开头)、#44(有 #);

合法的有:miles、Test、$4、apps

关键字有:class、public、int

2.5 指出并修改下面代码中的错误:

public class Test {public static void main(String[] args) {int i = k + 2;System.out.println(i);}}

解:

变量k没有被赋值。

2.6 请指出并修改下面代码中的错误:

public class Test {public static void main(String[] args) {int i = j = k = 2;System.out.println(i + " " + j + " " + k);}}

解:

j 和 k 没有声明变量。

其中一种正确修改如下

public class Test {public static void main(String[] args) {int i, j, k;i = j = k = 2;System.out.println(i + " " + j + " " + k);}}

2.7 使用常量的好处是什么?声明一个int类型的常量SIZE,并且值为20。

解:

使用常量有三个好处:

不必重复输入同一个值;如果必须修改常量值(例如,将PI的值从 3.14 改为 3.14159),只需在源代码中的一个地方做改动;给常量赋一个描述性名字会提高程序易读性。

final int SIZE = 20;

2.8 类名、方法名、常量和变量的命名习惯是什么?按照Java的命名习惯,以下哪些项可以作为常量、方法、变量或者一个类?

解:

变量和方法名:使用小写字母命名。如果一个名字包含多个单词,就将它们连在一起,第一个单词的字母为小写,而后面的每个单词的首字母为大写。例如变量 radius 和 area 以及方法 print()。类名:每个单词的首字母大写,例如,类名 ComputeArea 和 System。常量:所有字母大写,并且两个单词间用下划线_连接,例如,常量 PI 和常量 MAX_VALUE。

2.9 将以下算法翻译成 Java 代码。

第一步:声明一个双精度型变量 miles,初始值为 100。

第二步:声明一个双精度型常量 KILOMETERS_PRE_MILE,初始值为 1.609。

第三步:声明一个双精度变量 kilometers,将 miles 和 KILOMETERS_PRE_MILE 相乘,并且将结果赋值给 kilometers。

第四步:在控制台显示 kilometers。

第四步之后,kilometers 是多少?

解:

代码如下

public class Test2_9 {public static void main(String[] args) {double miles = 100;final double KILOMETERS_PRE_MILE = 1.609;double kilometers = miles * KILOMETERS_PRE_MILE;System.out.println(kilometers);}}

2.10 找到最大和最小的 byte、short、int、long、float 以及 double。这些数据类型中,哪个需要的内存最小?

解:

从大到小依次为:double > float > long > int > short > byte

各自所占字节大小为:8 字节、4字节、8字节、4字节、2字节、1字节

即 byte 需要的内存最小。

2.11 给出以下求余计算的结果。

56 % 6

78 % -4

-34 % 5

-34 % -5

5 % 1

1 % 5

解:

代码如下

public class Test2_11 {public static void main(String[] args) {System.out.println(56 % 6);System.out.println(78 % -4);System.out.println(-34 % 5);System.out.println(-34 % -5);System.out.println(5 % 1);System.out.println(1 % 5);}}

结果如下

2,2,-4,-4,0,1

2.12 假设今天是周二,100天后将是周几?

解:

代码如下

public class Test2_12 {public static void main(String[] args) {System.out.println("是周" + (2 + 100) % 7);}}

是周 4

2.13、25 / 4 的结果是多少?如果你希望得到浮点数结果,如何重写表达式?

解:

25 / 4 的结果是 6;想要得到浮点数结果的话,表达式应改为 25.0 / 4

2.14 给出以下代码的结果:

System.out.println(2 * (5 / 2 + 5 / 2));System.out.println(2 * 5 / 2 + 2 * 5 / 2);System.out.println(2 * (5 / 2));System.out.println(2 * 5 / 2);

解:

结果如下

8,10,4,5

2.15 下面的语句正确吗?如果正确的话,给出输出。

System.out.println("25 / 4 is " + 25 /4);System.out.println("25 / 4.0 is " + 25 / 4.0);System.out.println("3 * 2 / 4 is " + 3 * 2 / 4);System.out.println("3.0 * 2 / 4 is " + 3.0 * 2 / 4);

解:

这些语句都是正确的。输出如下

25 / 4 is 625 / 4.0 is 6.253 * 2 / 4 is 13.0 * 2 / 4 is 1.5

2.16 写一个显示 2 ^ 3.5 的计算结果的语句。

解:

Math.pow(2, 3.5);

2.17 假设 m 和 r 是整数。编写一个 Java 表达式,使得 mr^2 可以得到一个浮点数类型的结果。

解:

double result = m * r * r;

2.18 在 float 和 double 类型的变量中保存了多少个精确位?

解:

float保存了 7 到 8 位;double 保存了 15 到 17 位。

2.19 以下哪些是正确的浮点数类型直接量?

12.3、12.3e+2、23.4e-2、-334.4、20.5、39F、40D

解:

12.3(是)、12.3e+2(是)、23.4e-2(是)、-334.4(是)、20.5(是)、39F(不是)、40D(不是)

2.20 以下哪些和 52.534 是等价的?

5.2534e+1、0.52534e+2、525.34e-1、5.2534e+0

解:

前三个是等价的,最后一个不等价。

2.21 以下哪些是正确的直接量?

5_2534e+1、2534、5_2、5

解:

前三个正确,最后一个错误。

2.22 如何在 Java 中表达以下算数表达式?

a.

b.

解:

表达式如下

a、

4 / 3 * (r + 34) - 9 * (a + b * c) + (3 + d * (2 + a)) / (a + b * d);

b、

5.5 * Math.pow((r + 2.5), (2.5 + 1));

2.23 如何获得当前的秒、分钟以及小时数?

解:

2-7程序清单那个太过基础,有一个工作中用到的程序如下:

public class Test2_23 {public static void main(String[] args) {LocalDateTime localDateTime = LocalDateTime.now();String format = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));System.out.println(format);}}

2.24 给出以下代运行的结果:

double a = 6.5;a += a + 1;System.out.println(a);a = 6;a /= 2;System.out.println(a);

解:

结果如下

14.0

3.0

2.25 下面的说法哪个为真?

a.任何表达式都可以用作一个语句。

b.表达式 x++ 可以用作一个语句。

c.语句 x = x + 5 也是一个表达式。

d. x = y = x = 0 是非法的。

解:

abc为真,d为假。

2.26 给出以下代码的输出:

解:

代码和结果如下

int a = 6;int b = a++;System.out.println(a); // 结果为 7System.out.println(b); // 结果为 6a = 6;b = ++a;System.out.println(a); // 结果为 7System.out.println(b); // 结果为 7

2.27 在一次计算中,各种类型的数值可以一起使用吗?

解:

可以。

2.28 将一个 double 类型数值显式类型转换为 int 时,是如何处理 double 值的小数部分的?类型转换改变被类型转换的变量吗?

解:

直接去除小数部分,保留整数部分;不改变。

2.29 给出以下代码片段的输出:

解:

代码及其结果如下

float f =12.5F;int i = (int)f;System.out.println("f is " + f); // f is 12.5System.out.println("i is " + i); // i is 12

2.30 在程序清单 2-8 中,如果将第 11 行的 (int)(tax×100)/100.0 改为 (int)(tax×100)/100,对于输入的购买量 197.55,输出会是什么?

解:

输出会是 11.853

2.31 给出以下代码的输出:

解:

double amount = 5;System.out.println(amount / 2); //结果是 2.5System.out.println(5 / 2); //结果是 2

2.32 如何编写下面的数学表达式的代码?

解:

(-b + Math.pow((b*b - 4*a*c), 0.5)) / (2*a);

2.33 给出输入值为 1.99 的输出。

解:

Your amount 1.99 consists of1 dollars3 quarters2 dimes0 nickels4 pennies

2.34 可以将一个变量声明为 int 类型,之后重新将其生命为 double 类型吗?

解:

不可以。

2.35 什么是整数溢出?浮点数操作会导致溢出吗?

解:

当一个变量被赋予一个过大的值,以至于无法存储该值,这称为溢出;

会,很小的话,会引起向下溢出。

2.36 溢出会导致一个运行时错误吗?

解:

会。

2.37 什么是取整错误?整数操作会导致取整错误吗?浮点数操作会导致取整错误吗?

解:

取整错误:也称为凑数错误,是在计算得到的数字的近似值和确切的算数值之间的不同;

不会;

会。

编程练习题

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

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

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

下面是一个运行示例:

Enter a degree in Celsius: 4343.0 celsius is 109.4 fahrenheit

解:代码如下

public class Exercise2_1 {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.512The area is 95.0330975The volume is 1140.39717

解:

代码如下

public class Exercise2_2 {public static void main(String[] args) {final double PI = 3.14159;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 area = radius * radius * PI;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 of feet: 16.516.5 feet is 5.0325 meters

解:代码如下

public class Exercise2_3 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter a value of feet: ");double feet = input.nextDouble();double meter = feet * 0.305;System.out.println(pounds + " feet is " + meter + " meters");}}

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

Enter a number in pounds 55.555.5 pounds is 25.197 kilograms

解:代码如下

public class Exercise2_4 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter a number in pounds ");double pounds = input.nextDouble();double kilograms = pounds * 0.454;System.out.println(pounds + " pounds is " + kilograms + " kilograms");}}

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

Enter the subtotal and a gratuity rate: 1015The gratuity is $1.5 and total is $11.5

解:代码如下

public class Exercise2_5 {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 rate = input.nextDouble() / 100;double gratuity = subtotal * rate;double total = gratuity + subtotal;System.out.println("The gratuity is $" + gratuity + " and total is $" + total);}}

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

提示:利用操作符 % 分解数字,然后使用操作符 / 去掉分解出来的数字。例如:932 % 10 = 2,932 / 10 = 93。下面是一个运行示例:

Enter a number between 0 and 1000: 999The sum of the digits is 27

解:代码如下

public class Exercise2_6 {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 number_1 = number % 10;int number_2 = number_1 % 10;int number_3 = number_2 % 10;int sum = number_1 + number_2 + number_3;System.out.println("The sum of the digits is " + sum);}}

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

Enter the number of minutes: 10000000001000000000 minutes is approximately 1902 years and 214 days

解:代码如下

public class Exercise2_7 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter the number of minutes: ");int minutes = input.nextInt();int days = minutes / 60 / 24;int years = days / 365;int surplusDays = days % 365;System.out.println(minutes + " minutes is approximately " + years + " years and " + surplusDays + " days");}}

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

请输入时区偏移量:8当前时区的时间为 18 31 39

解:代码如下

public class Exercise2_8 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("请输入时区偏移量:");int timeZoneOffset = input.nextInt();// 获取自1970年1月1日午夜以来的总毫秒数long totalMilliseconds = System.currentTimeMillis();// 获取自1970年1月1日午夜以来的总毫秒数long totalSeconds = totalMilliseconds / 1000;// 计算这个时间中剩余的秒数long currentSecond = totalSeconds % 60;// 获得总分钟数long totalMinutes = totalSeconds / 60;// 计算这个时间中剩余的分钟数long currentMinute = totalMinutes % 60;// 获得这个时间中的小时数long totalHours = totalMinutes / 60;// 计算当前时区的时间long currentHour = (totalHours + timeZoneOffset) % 24;// 显示结果System.out.println("当前时区的时间为 " + currentHour + " "+ currentMinute + " " + currentSecond);}}

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

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

Enter V0, V1, and t: 5.5 50.9 4.5The average acceleration is 10.0889

解:代码如下

public class Exercise2_9 {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 a = (v1 - v0) / t;System.out.println("The average acceleration is " + a);}}

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

Q = M ×(最终温度 - 初始温度)× 4184

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

Enter the amount of water in kilograms: 55.5请输入水的初始温度:3.5请输入水的最终温度:10.5能量需要:1625484.0

解:代码如下

public class Exercise2_10 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter the amount of water in kilograms: ");double amountOfWater = input.nextDouble();System.out.print("请输入水的初始温度:");double initialTemperature = input.nextDouble();System.out.print("请输入水的最终温度:");double finalTemperature = input.nextDouble();double q = amountOfWater * (finalTemperature - initialTemperature) * 4184;System.out.print("能量需要:" + q);}}

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

Enter the number of years: 5The population in 5 years is 325932970

解:代码如下

public class Exercise2_11 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter the number of years: ");int years = input.nextInt();System.out.println("The population in " + years + " years is "+ (312032486 + (years * 365 * 24 * 60 * 60 / 7) - (years * 365 * 24 * 60 * 60 / 13)+ (years * 365 * 24 * 60 * 60 / 45)));}}

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

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

Enter speed and acceleration: 60 3.5The minimum runway length for this airplane is 514.286

解:代码如下

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

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

100 * (1 + 0.0047) = 100.417

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

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

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

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

以此类推。

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

Enter the monthly saving amount: 100After the six month, the account value is 608.8181155768638

解:

public class Exercise2_13 {public static void main(String[] args) {Scanner in = new Scanner(System.in);System.out.print("Enter the monthly saving amount: ");double amount = in.nextDouble();double rateOfMonth = 1 + 0.00417;double totalOfMonth1 = amount * rateOfMonth;double totalOfMonth2 = (amount + totalOfMonth1) * rateOfMonth;double totalOfMonth3 = (amount + totalOfMonth2) * rateOfMonth;double totalOfMonth4 = (amount + totalOfMonth3) * rateOfMonth;double totalOfMonth5 = (amount + totalOfMonth4) * rateOfMonth;double totalOfMonth6 = (amount + totalOfMonth5) * rateOfMonth;System.out.println("After the six month, the account value is " + totalOfMonth6);}}

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

注意:一磅是 0.45359237 公斤,一英寸是 0.0254 米。下面试一个运行示例:

Enter weight in pounds: 95.5Enter height in inches: 50BMI is 26.857257942215885

解:代码如下

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

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

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

Enter xl and yl: 1.5 -3.4Enter x2 and y2: 4 5The distance between the two points is 8.764131445842194

解:代码如下

public class Exercise2_15 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter xl and yl: ");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(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2), 0.5);System.out.println("The distance between the two points is " + distance);}}

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

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

Enter the side: 5.5The area of the hexagon is 78.5918

解:代码如下

public class Exercise2_16 {public static void main(String[] args) {Scanner in = new Scanner(System.in);System.out.print("Enter the side: ");double s = in.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) 利用温度和风速计算新的风寒温度,来衡量寒冷程度。计算公式如下所示:

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

编写程序,提示用户输入在 -58 T和 41 T之间的度数,同时大于或等于 2 的风速,然后显

示风寒温度。使用 Math.pow(a.b)来计算 vew。下面是一个运行示例:

Enter the temperature in Fahrenheit between -58℉ and 41℉: 5.3Enter the wind speed (>=2) in miles per hour: 6The wind chill index is -5.56707

解:代码如下

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

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

解:代码如下

public class Exercise2_18 {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));}}

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

下面是一个运行示例:

Enter three points for a triangle: 1.5 -3.4 4.6 5 9.5 -3.4The area of the triangle is 33.6

解:代码如下

public class Exercise2_19 {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 s1 = Math.pow(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2), 0.5);double s2 = Math.pow(Math.pow(x3 - x2, 2) + Math.pow(y3 - y2, 2), 0.5);double s3 = Math.pow(Math.pow(x1 - x3, 2) + Math.pow(y1 - y3, 2), 0.5);double s = (s1 + s2 +s3) / 2;double area = Math.pow(s * (s - s1) * (s - s2) * (s - s3), 0.5);System.out.println("The area of the triangle is " + area);}}

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

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

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

解:代码如下

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

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

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

下面是一个运行示例:

Enter investment amount: 1000.56Enter annual interest rate in percentage: 4.25Enter number of years: 1Accumulated value is $1043.337716309617

解:代码如下

public class Exercise2_21 {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 total = 1000 * Math.pow(1 + rate / 1200, years * 12);System.out.println("Accumulated value is $" + total);}}

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

解:代码如下

public class Exercise2_22 {public static void main(String[] args) {// Create a ScannerScanner input = new Scanner(System.in);// Receive the amountSystem.out.println("Enter an amount in int, for example 1156");int remainingAmount = input.nextInt();// Find the number of one dollarsint numberOfOneDollars = remainingAmount / 100;remainingAmount = remainingAmount % 100;// Find the number of quarters in the remaining amountint numberOfQuarters = remainingAmount / 25;remainingAmount = remainingAmount % 25;// Find the number of dimes in the remaining amountint numberOfDimes = remainingAmount / 10;remainingAmount = remainingAmount % 10;// Find the number of nickels in the remaining amountint numberOfNickels = remainingAmount / 5;remainingAmount = remainingAmount % 5;// Find the number of pennies in the remaining amountint numberOfPennies = remainingAmount;// Display resultsSystem.out.println("Your amount" + remainingAmount + " 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(驾驶费用)编写一个程序,提示用户输人驾驶的距离、以每加仑多少英里的汽车燃油性能,以及每加仑的价格,然后显示旅程的费用。下面是一个运行示例:

Enter the driving distance: 900.5Enter miles per gallon: 25.5Enter price per gallon: 3.55The cost of driving is $125.36

解:代码如下

public class Exercise2_23 {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();System.out.println("The cost of driving is $" + distance / miles * price);}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。