2022-12-14&15习题第二章-创新互联
指出并修改以下代码中的错误:
目前成都创新互联公司已为千余家的企业提供了网站建设、域名、雅安服务器托管、网站托管运营、企业网站设计、高阳网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。public class Test{public void main(string[] args){ double i = 50.0;
double k = double i + 50.0;
double j = k + 1;
System.out.println("j is " + j + " and
k is " + k);
}
}
public class Test{public static void main(String[] args){double i = 50.0;
double k = i + 50.0;
double j = k + 1;
System.out.println("j is " + j + " and k is " + k);
}
}
2.3习题如何编写一条语句,让用户从键盘输人一个双精度值? 在执行下面代码的时候,如果你输入5a, 将发生什么?
将发生一个运行时错误
下面两个import语句之间有什么执行的不同吗?
Import java.util.Scanner;
Import java.util.*;
2.4习题.Scanner是明确导入,导入util包中Scanner类;.*是通配符导入,导入util包中所有的类
以下标识符哪些是合法的?哪些是Java的关键字?
合法的:miles、Test、$4、x、y、radius、apps
非法的:a++、–a、4#R、#44、int
关键/保留字:class、public
2.5习题请指出并修改下面代码中的错误:
int i = k + 2;
System.out.println(i);
int k = 0;
int i = k + 2;
System.out.println(i);
2.6习题请指出并修改下面代码中的错误:
int i = j = k = 2;
int j;
int k;
int i = j = k = 2;
2.8习题使用常量的好处是什么? 声明一个int类型的常量SIZE, 并且值为20。
可以不必频繁的写数值,代码改动只需修改一处
final int SIZE = 20;
类名、方法名、常量和变量的命名习惯是什么? 按照Java的命名习惯,以下哪些项可以作为常量、方法 、变量或者一个类?
类名:每个单词首字母大写,直接连接
变量和方法:第一个单词小写,后面每一个单词首字母大写,直接连接
常量:全部大写,下划线连接
类名:Test
变量和方法:read、readDouble
常量:MAX_VALUE
将以下算法翻译成Java代码
第一步:声明一个双精度型变量miles, 初始值为100
第二步:声明一个双精度型常量KILOMETERS_PER_MILE, 初始值1.609
第三步:声明一个双精度型变量kilometers, 将miles和KILOMETERS_PER_MILE相乘,并且将结果赋值给 kilometers
第四步:在控制台显示 kilometers
第四步之后,kilometers 是多少?
public class Main{public static void main(String[] args){double miles = 100;
final double KILOMETERS_PER_MILE = 1.609;
double kilometers = miles * KILOMETERS_PER_MILE;
System.out.print(kilometers);
}
}
160.9
2.9习题找到大和最小的byte、short、int、long、float以及double。这些数据类型中,哪个需要的内存最小?
byte需要的内存最小
给出以下求余计算的结果。
相对简单
假设今天是周二,100天后将是周几?
(2 + 100)% 7 = 2,所以是星期4
25/4的结果是多少? 如果你希望得到浮点数结果,如何重写表达式?
6,需要将至少一个数字改成浮点数
给出以下代码的结果:
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
下面的语句正确吗? 如果正确的话,给出输出
System.out.printIn("25 / 4 is " + 25 / 4);
System.out.printIn("25 / 4.0 is " + 25 / 4.0);
System.out.println("3.0 * 2 / 4 is " + 3 * 2 / 4);
System.out.println("3.0 * 2 / 4 is " + 3.0 * 2 / 4);
25 / 4 is 6
25 / 4.0 is 6.25
3.0 * 2 / 4 is 1
3.0 * 2 / 4 is 1.5
写一个显示 2 3.5 2^{3.5} 23.5的计算结果的语句
public class Main{public static void main(String[] args){System.out.println(Math.pow(2, 3.5));
}
}
假设m和r是整数。编写一个Java表达式,使得 m r 2 mr^2 mr2可以得到一个浮点数类型的结果
public class Main{public static void main(String[] args){//已经指定了double,进行运算的结果是小数
//没有变量,直接打印结果,必须至少有1位是小数,结果才是小数
double m = 1;
double r = 2;
System.out.print(m * r * r);
}
}
4.0
2.10习题在float和double类型的变量中保存了多少个精确位?
float中有7-8小数位,double有15-17小数位
以下哪些是正确的浮点数类型直接量?
全都是 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.11习题正确:
5.2534e+1,
5_2
错误:
_2534,
5_
如何在 Java 中表达以下算术表达式?
4 3 ( r + 34 ) − 9 ( a + b c ) + 3 + d ( 2 + a ) a + b d \frac{4}{3(r + 34)} - 9(a + bc) + \frac{3 + d(2+a)}{a+bd} 3(r+34)4−9(a+bc)+a+bd3+d(2+a)
$5.5 $ x $ (r+2.5)^{2.5 + t}$
public class Main{public static void main(String[] args){int a;
int b;
int c;
int d;
int r;
int t = a = b = c = d = r = 1;
System.out.println(4/(3 * (r+34)) - 9 * (a + b * c) + (3 + d * (2 + a)) / (a + b * d));
System.out.print(5.5 * Math.pow(r + 2.5, 2.5 + t));
}
}
-15
441.164791259315
2.12 习题public class Main{public static void main(String[] args){//读取当前毫秒时间
long totalMilliseconds = System.currentTimeMillis();
//计算总秒数
long totalSeconds = totalMilliseconds / 1000;
//计算当前秒数
long currentSeconds = totalSeconds % 60;
//计算总分钟数
long totalMinutes = totalSeconds / 60;
//计算当前分钟数
long currentMinutes = totalMinutes % 60;
//计算总小时数
long totalHours = totalMinutes / 60;
//计算当前小时数
long currentHours = totalHours % 24;
System.out.print("The current time is " + currentHours + ":" + currentMinutes + ":" + currentSeconds);
}
}
2.13习题给出以下代码运行的结果:
double a = 6.5;
a = a+1;
System.out.println(a);
a = 6;
a /= 2;
System.out.println(a);
7.5
3.0
2.14习题下面的说法哪个为真?
a.任何表达式都可以用作一个语句。
b.表达式 x++ 可以用作一个语句。
c.语句x = x + 5也是一个表达式
d.x = y = x = 0是非法的。
a、b、c正确,d是合法的
给出以下代码的输出
public class Main{public static void main(String[] args){int a = 6;
int b = a++;
System.out.println(a);
System.out.println(b);
int c = 6;
int d = ++c;
System.out.println(c);
System.out.println(d);
}
}
7
6
7
7
2.15习题在一次计算中,各种类型的数值可以一起使用吗?
可以
将一个double类型数值显式类型转换为int时,是如何处理double值的小数部分的? 类型转换改变被类型转换的变量吗?
直接抹去小数部位;不改变
给出以下代码片段的输T出:
float f = 12.5F;
int i = (int)f;
System.out.println("f is " + f);
System.out.println("i is " +i);
f is 12.5
i is 12
给出以下代码的输出:
double amount = 5;
System.out.println(amount / 2);
System.out.println(5 / 2);
2.5
2
2.16习题如何编写下面的数学表达式的代码?
− b + b 2 − 4 a c 2 a \frac{-b + \sqrt{b^2 - 4ac}}{2a} 2a−b+b2−4ac
public class Main{public static void main(String[] args){int a = 1;
int b = 4;
int c = 2;
System.out.println((-b + Math.sqrt(Math.pow(b, 2) - 4 * a * c)) / (2 * a));
}
}
2.17习题import java.util.Scanner;
public class Main{public static void main(String[] args){//1)
Scanner input = new Scanner(System.in);
System.out.print("Please type your total money: ");
double amount = input.nextDouble();
System.out.println("You have " + amount + " dollars");
//2)
int remainingAmount = (int)(amount * 100);
//3)
int currentDollar = remainingAmount / 100;
remainingAmount = remainingAmount % 100;
//4)
int currentQuarter = remainingAmount / 25;
remainingAmount = remainingAmount % 25;
//5)
int currentDime = remainingAmount / 10;
remainingAmount = remainingAmount % 10;
//6)
int currentNickel = remainingAmount / 5;
remainingAmount = remainingAmount % 5;
//7)
System.out.println("You have " + currentDollar + " dollars");
System.out.println("You have " + currentQuarter + " quarters");
System.out.println("You have " + currentDime + " dimes");
System.out.println("You have " + currentNickel + " nickels");
System.out.println("You have " + remainingAmount + " pennies");
input.close();
}
}
Please type your total money: 1.99
You have 1.99 dollars
You have 1 dollars
You have 3 quarters
You have 2 dimes
You have 0 nickels
You have 4 pennies
2.18习题可以将一个变量声明为int类型,之后重新将其声明为double类型吗?
int a = 5;
double b = (double)a;
什么是整数溢出? 浮点数操作会导致溢出吗?
数字大小超过了当前数据类型的限度;会,但是一般情况不用考虑
溢出会导致一个运行时错误吗?
会
什么是取整错误? 整数操作会导致取整错误吗? 浮点数操作会导致取整错误吗?
编程练习题计算得到的数字精确值与算数值的差异;整数运算不会导致错误;浮点数会
(将摄氏温度转换为华氏温度)编写程序,从控制台读人double型的摄氏温度,然后将其转换为华氏温度 ,并且显示结果。转换公式如下所示:
华氏温度 = (9/5) * 摄氏温度 + 32
import java.util.Scanner;
public class Main{public static void main(String[] args) {Scanner input = new Scanner(System.in);
System.out.print("Please input your Celsius temperature: ");
double celsius = input.nextDouble();
double fahrenheit = (9.0 / 5) * celsius + 32;
System.out.println(fahrenheit);
input.close();
}
}
Please input your Celsius temperature: 43
109.4
(计算圆柱体的体积)编写程序,读人圆柱体的半径和高,并使用下列公式计算圆柱的体积:面积=半径 x 半径 x p;体积=面积 X 高
import java.util.Scanner;
public class Main{public static void main(String[] args) {Scanner input = new Scanner(System.in);
System.out.print("Please input cylinder's radius, and then length: ");
double radius = input.nextDouble();
double length = input.nextDouble();
double area = radius * radius * Math.PI;
double volume = area * length;
System.out.println("The area is " + area + " and the volume is " + volume);
input.close();
}
}
Please input cylinder's radius, and then length: 5.5 12
The area is 95.03317777109125 and the volume is 1140.398133253095
(将英尺转换为米)编写程序,读人英尺数,将其转换为米数并显示结果。一英尺等于0.305米
import java.util.Scanner;
public class Main{public static void main(String[] args) {Scanner input = new Scanner(System.in);
System.out.print("Please input feet: ");
double feet = input.nextDouble();
double meter = feet * 0.305;
System.out.println(feet + " feets equal to " + meter + " meters");
input.close();
}
}
Please input feet: 16.5
16.5 feets equal to 5.0325 meters
(将磅转换为千克)编写程序,将磅数转换为千克数。程序提示用户输人磅数,然后转换成千克并显示结果。一磅等于 0.454 千克。
import java.util.Scanner;
public class Main{public static void main(String[] args) {Scanner input = new Scanner(System.in);
System.out.print("Please input pounds: ");
double pounds = input.nextDouble();
double kilograms = pounds * 0.454;
System.out.println(pounds + " pounds equal to " + kilograms + " kilograms");
input.close();
}
}
Please input pounds: 55.5
55.5 pounds equal to 25.197 kilograms
(财务应用程序:计算小费)编写一个程序,读入一笔费用与酬金率,计算酬金和总钱数。例如, 如果用户输人10 作为费用,15%作为酬金率,计算结果显示酬金为 $1.5, 总费用为11.5。
import java.util.Scanner;
public class Main{public static void main(String[] args) {Scanner input = new Scanner(System.in);
System.out.print("Please input subtotal, and then gratuity rate: ");
double subtotal = input.nextDouble();
double gratuityRate = input.nextDouble();
double gratuity = (subtotal * gratuityRate) / 100;
double total = gratuity + subtotal;
System.out.println("The gratuity is " + gratuity + " and the total is " + total);
input.close();
}
}
Please input subtotal, and then gratuity rate: 10 15
The gratuity is 1.5 and the total is 11.5
(求一个整数各位数的和)编写程序,读取一个在0和1000之间的整数,并将该整数的各位数字相加。例如:整数是932, 各位数字之和为14
import java.util.Scanner;
public class Main{public static void main(String[] args) {Scanner input = new Scanner(System.in);
System.out.print("Please input a number: ");
int number = input.nextInt();
int divide1 = number / 10;
int divide2 = number % 10;
int divide3 = divide1 / 10;
int divide4 = divide1 % 10;
int total = divide2 + divide3 + divide4;
System.out.println(total);
input.close();
}
}
Please input a number: 589
22
(求出年数)编写程序,提示用户输入分钟数(例如十亿)然后显示这些分钟代表多少年和多少天。为了简化问题,假设一年有 365 天
import java.util.Scanner;
public class Main{public static void main(String[] args) {Scanner input = new Scanner(System.in);
System.out.print("Please input minutes: ");
int minutes = input.nextInt();
int years = minutes / 525600;
int remainingMinutes = minutes % 525600;
int days = remainingMinutes / 1440;
System.out.println(minutes + " minutes equal to " + years + " years and " + days + " days.");
input.close();
}
}
Please input minutes: 1000000000
1000000000 minutes equal to 1902 years and 214 days.
(当前时间)程序清单2-7给出了显示当前格林威治时间的程序。修改这个程序,提示用户输入相对于 GMT 的时区偏移量,然后显示在这个特定时区的时间。
import java.util.Scanner;
public class Main{public static void main(String[] args){//读取当前毫秒时间
long totalMilliseconds = System.currentTimeMillis();
//计算总秒数
long totalSeconds = totalMilliseconds / 1000;
//计算当前秒数
long currentSeconds = totalSeconds % 60;
//计算总分钟数
long totalMinutes = totalSeconds / 60;
//计算当前分钟数
long currentMinutes = totalMinutes % 60;
//计算总小时数
long totalHours = totalMinutes / 60;
//计算当前小时数
long currentHours = totalHours % 24;
Scanner input = new Scanner(System.in);
System.out.println("Please input your local timeZone compared to GMT with +/- integers: ");
int timeZone = input.nextInt();
long newHours = currentHours + timeZone;
System.out.print("The current time is " + newHours + ":" + currentMinutes + ":" + currentSeconds);
}
}
Please input your local timeZone compared to GMT with +/- integers:
8
The current time is 20:53:44
(物理:加速度)平均加速度定义为速度的变化量除以这个变化所用的时间,如下式所示: a = v 1 − v 0 t a = \frac{v_1 - v_0}{t} a=tv1−v0 编写程序,提示用户输人以米/秒为单位的起始速度 v 0 v_0 v0, 以米/秒为单位的终止速度 v 1 v_1 v1,以及以秒为单位的时间段t, 最后显示平均加速度
import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner input = new Scanner(System.in);
System.out.print("Please input v_0, v_1, and t: ");
double v_0 = input.nextDouble();
double v_1 = input.nextDouble();
double t = input.nextDouble();
System.out.print("The acceleration is " + (v_1 - v_0) / t);
}
}
Please input v_0, v_1, and t: 5.5 50.9 4.5
The acceleration is 10.088888888888889
(科学:计算能量)编写程序,计算将水从初始温度加热到最终温度所需的能量。程序应该提示用户输人水的重量(以千克为单位 ),以及水的初始温度和最终温度。计算能量的公式是: Q = M x (最终温度 - 初始温度) x 4184,这里的M是以千克为单位的水的重量,温度以摄氏度为单位,而能置Q以焦耳为单位。
import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner input = new Scanner(System.in);
System.out.print("Please input waterQuantity: ");
double waterquantity = input.nextDouble();
System.out.print("Please input initial temp: ");
double tem0 = input.nextDouble();
System.out.print("Please input final temp: ");
double tem1 = input.nextDouble();
System.out.print("The energy needed is " + waterquantity * (tem1 - tem0) * 4184);
}
}
Please input waterQuantity: 55.5
Please input initial temp: 3.5
Please input final temp: 10.5
The energy needed is 1625484.0
(人口统计)重写编程练习题1.11, 提示用户输人年数,然后显示这个年数之后的人口值。将编程练习题 1.11中的提示用于这个程序。人口数应该类型转换为一个整数
import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner input = new Scanner(System.in);
System.out.print("Please input the years: ");
int years = input.nextInt();
System.out.println("第" + years + "年:" + 365 * years * (86400/7 - 86400/13 + 86400/45));
}
}
Please input the years: 5
第5年:13899200
(物理:求出跑道长度)假设一个飞机的加速度是a,而起飞速度是v,那么可以使用下面的公式计算出飞机起飞所需的最短跑道长度: 跑道长度 = v 2 2 a \frac{v^2}{2a} 2av2。编写程序,提示用户输人以米/秒(m/s) 为单位的速度v和以米/秒的平方(m/s2 ) 为单位的加速度a, 然后显示最短跑道长度。
import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner input = new Scanner(System.in);
System.out.print("Please input speed and acceleration: ");
double speed = input.nextDouble();
double acceleration = input.nextDouble();
System.out.println("The minumum length for taking off is: " + speed * speed / (2 * acceleration) );
}
}
Please input speed and acceleration: 60 3.5
The minumum length for taking off is: 514.2857142857143
(财务应用程序:复利值)假设你每月向银行账户存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,依此类推。编写程序显示六个月后账户上的钱数。
import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner input = new Scanner(System.in);
System.out.print("Please input monthly saving: ");
double saving = input.nextDouble();
double total1 = saving * 1.00417;
double total2 = (saving + total1) * 1.00417;
double total3 = (saving + total2) * 1.00417;
double total4 = (saving + total3) * 1.00417;
double total5 = (saving + total4) * 1.00417;
double total6 = (saving + total5) * 1.00417;
System.out.println("The 6th saving is: " + total6);
}
}
Please input monthly saving: 100
The 6th saving is: 608.8181155768638
(医疗应用程序:计算 BMI)身体质量指数(BMI)是对体重的健康测量。它的值可以通过将体重(以公斤为单位)除以身高(以米为单位)的平方值得到。编写程序,提示用户输人体重(磅为单位)以及身髙(英寸为单位),然后显示BMI。注意:一磅是0.45359237公斤,一英寸是 0.0254 米
import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner scanner = new Scanner(System.in);
System.out.print("Input height in inches: ");
double height = scanner.nextDouble();
System.out.print("Input weight in pounds: ");
double weight = scanner.nextDouble();
double BMI = (weight * 0.45359237)/Math.pow(height * 0.0254, 2);
System.out.printf("Your BMI is " + BMI);
}
}
Input height in inches: 50
Input weight in pounds: 95.5
Your BMI is 26.857257942215885
(几何:两点间距离)编写程序,提示用户输入两个点(x1,y1)和(x2,y2),然后显示两点间的距离。计算两点间距离的公式是 ( x 2 − x 1 ) 2 − ( y 2 − y 1 ) 2 \sqrt{(x_2 - x_1)^2-(y_2 - y_1)^2} (x2−x1)2−(y2−y1)2 。注意,可以使用Math.pow(a, 0.5)来计算$\sqrt{a}
import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner scanner = new Scanner(System.in);
System.out.print("Input x1, x2, y1, y2 accordingly: ");
double x1 = scanner.nextDouble();
double x2 = scanner.nextDouble();
double y1 = scanner.nextDouble();
double y2 = scanner.nextDouble();
System.out.print(Math.pow(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2), 0.5));
}
}
Input x1, x2, y1, y2 accordingly: 1.5 4 -3.4 5
8.764131445842194
(几何:六边形面积)编写程序,提示用户输入六边形的边长,然后显示它的面积。计算六边形面积的公式是: 面积 = 3 3 2 s 2 \frac{3\sqrt3}{2}s^2 233 s2 , s 就是边长。
import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner input = new Scanner(System.in);
System.out.print("Input side: ");
double side = input.nextDouble();
System.out.print("The area is " + ((3 * Math.pow(3, 0.5))/2) * side * side);
input.close();
}
}
Input side: 5.5
The area is 78.59180539343781
(科学:风寒温度)外面到底有多冷?只有温度是不足以提供答案的,包括风速、相对湿度以及阳光等其他的因素在确定室外是否寒冷方面都起了很重要的作用。2001年,国家气象服务(NWS)利用温度和风速计算新的风寒温度,来衡量寒冷程度,计算公式如下所示:
t w c = 35.74 + 0.6215 t a − 35.75 v 0 . 16 + 0.4275 t a v 0 . 16 t_{wc} = 35.74 + 0.6215t_a - 35.75v^0.16 + 0.4275t_av^0.16 twc=35.74+0.6215ta−35.75v0.16+0.4275tav0.16
这里的 t a t_a ta是室外的温度,以华氏摄氏度为单位,而v是速度,以每小时英里数为单位。 t w c t_{wc} twc是风寒温度。该公式不适用于风速低于2mph,或温度在-58以下或41以上的情况。
编写程序,提示用户输入在-58和41之间的度数,同时大于或等于2的风速,然后显示风寒温度。
import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner input = new Scanner(System.in);
System.out.print("Enter temperature(Fahrenheit, between -58 and 41), and wind spped(>= 2): ");
double temp = input.nextDouble();
double speed = input.nextDouble();
System.out.print("The wind chill index is " + (35.74 + 0.6215 * temp - 35.75 * Math.pow(speed, 0.16) + 0.4275 * temp * Math.pow(speed, 0.16)));
input.close();
}
}
Enter temperature(Fahrenheit, between -58 and 41), and wind spped(>= 2): 5.3 6
The wind chill index is -5.567068455881625
(打印表格)编写程序,显示下面的表格。将浮点数值类型转换为整数。
public class Main{public static void main(String[] args){System.out.print("a");
System.out.print(" " + "b");
System.out.println(" " + "pow(a,b)");
int a = (int)(Math.pow(1, 2));
System.out.print(1);
System.out.print(" " + 2);
System.out.println(" " + a);
int b = (int)(Math.pow(2, 3));
System.out.print(2);
System.out.print(" " + 3);
System.out.println(" " + b);
int c = (int)(Math.pow(3, 4));
System.out.print(3);
System.out.print(" " + 4);
System.out.println(" " + c);
int d = (int)(Math.pow(4, 5));
System.out.print(4);
System.out.print(" " + 5);
System.out.println(" " + d);
int e = (int)(Math.pow(5, 6));
System.out.print(5);
System.out.print(" " + 6);
System.out.println(" " + e);
}
}
a b pow(a,b)
1 2 1
2 3 8
3 4 81
4 5 1024
5 6 15625
**(几何:三角形的面积)编写程序,提示用户输入三角形的三个点(x1,y1), (x2,y2)和(x3.y3), 然后显示它的面积。计算三角形面积的公式是: **
s= (边1+边2+边3)/2
面积 = s ( s − 边 1 ) ( s − 边 2 ) ( s − 边 3 ) \sqrt{s(s-边1)(s-边2)(s-边3)} s(s−边1)(s−边2)(s−边3)
import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner input = new Scanner(System.in);
System.out.print("Enter 3 points of an triangle: ");
//下面的6个数据输入是分先后顺序的
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 line12 = Math.pow(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2), 0.5);
double line13 = Math.pow(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2), 0.5);
double line23 = Math.pow(Math.pow(x3 - x2, 2) + Math.pow(y3 - y2, 2), 0.5);
double s = (line12 + line13 + line23) / 2;
double area = Math.pow(s * (s - line12) * (s - line23) * (s - line13), 0.5);
System.out.print("The area of this triangle is " + area);
input.close();
}
}
Enter 3 points of an triangle: 1.5 -3.4 4.6 5 9.5 -3.4
The area of this triangle is 33.600000000000016
(财务应用程序:计算利息)如果知道收支余额和年利率的百分比,就可以使用下面的公式计算下个月要支付的利息额: 编写程序,读取收支余额和年百分利率,显示两个版本的下月利息。
import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner input = new Scanner(System.in);
System.out.print("Enter balance and interest rate: ");
double balance = input.nextDouble();
double intRate = input.nextDouble();
System.out.print(balance * (intRate/1200));
input.close();
}
}
Enter balance and interest rate: 1000 3.5
2.916666666666667
(财务应用:计算未来投资值)编写程序,读取投资总额、年利率和年数,然后使用下面的公式显示未来投资金额
未来投资金额 = 投递总额 x ( 1 + 月利率 年数 ∗ 12 {1 + 月利率}^{年数 * 12} 1+月利率年数∗12)
import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner input = new Scanner(System.in);
System.out.print("Enter balance and interest rate: ");
double investment = input.nextDouble();
double annualrate = input.nextDouble();
double years = input.nextDouble();
//利率要接着/100
System.out.print(investment * Math.pow(1 + annualrate / 12 / 100, years * 12));
input.close();
}
}
Enter balance and interest rate: 1000.56
4.25
1
1043.9219854307503
(财务应用:货币单位)改写程序淸单2-10, 解决将double型值转换为int型值时可能会造成精度损失的问题。输人的输人值是一个整数,其最后两位代表的是美分币值。例如:1156 就表示的是11美元56美分
import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner input = new Scanner(System.in);
System.out.print("Enter total money as penny: ");
int penny = input.nextInt();
int dollar = penny / 100;
int remainingPenny = penny % 100;
System.out.print("You have " + dollar + " dollars and " + remainingPenny + " pennies");
input.close();
}
}
Enter total money as penny: 1156
You have 11 dollars and 56 pennies
(驾驶费用)编写一个程序,提示用户输人驾驶的距离、以每加仑多少英里的汽车燃油性能,以及每加仑的价格 ,然后显示旅程的费用
import java.util.Scanner;
public class Main{public static void main(String[] args){Scanner input = new Scanner(System.in);
System.out.print("Enter distance: ");
double distance = input.nextDouble();
System.out.print("Enter gallons: ");
double gallons = input.nextDouble();
System.out.print("Enter price: ");
double price = input.nextDouble();
System.out.print("Your cost will be " + distance / gallons * price);
input.close();
}
}
Enter distance: 900.5
Enter gallons: 25.5
Enter price: 3.55
Your cost will be 125.36372549019607
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
分享名称:2022-12-14&15习题第二章-创新互联
当前链接:http://ybzwz.com/article/cssoig.html