您的位置:首页 > 编程语言 > Java开发

Java语言程序设计(基础篇)(原书第10版) 练习题答案 第2章

2017-11-23 09:29 549 查看
基础篇课后习题答案,做了大部分,虽然不一定是最佳代码,但是保证每个都能运行,如有更好的答案,欢迎讨论

2.1 华氏温度转换

Scanner scanner = new Scanner(System.in);
System.out.print("请输入摄氏度:");

double calsius = scanner.nextDouble();

double fahrenheit = ((9 / 5D) * calsius) + 32D;

System.out.print("对应的华氏温度是:" + fahrenheit);


2.2 面积体积计算

Scanner scanner = new Scanner(System.in);

System.out.print("请输入圆柱体半径:");
double radius = scanner.nextDouble();

System.out.print("请输入圆柱体高度:");
double high = scanner.nextDouble();

double area = Math.pow(radius, 2) * Math.PI;
double volume = area * high;
System.out.println("圆柱体底面积是:" + area);
System.out.println("圆柱体体积是:" + volume);


2.3 英尺换算

Scanner scanner = new Scanner(System.in);

System.out.print("请输入英尺数:");
double feet = scanner.nextDouble();

double meter = feet * 0.305;

System.out.print(feet + "英尺是" + meter + "米");


2.5 酬金计算

Scanner scanner = new Scanner(System.in);

System.out.print("请输入费用:");
double fee = scanner.nextDouble();

System.out.print("请输入酬金率:");
double rewardPST = scanner.nextDouble();

double reward = fee * (rewardPST / 100D);
double totalFee = reward + fee;
System.out.println("酬金是:"+reward);
System.out.print("总费用是:"+totalFee);


2.6 位数相加

Scanner scanner = new Scanner(System.in);

System.out.print("请输入0到1000之间的整数:");
int num = scanner.nextInt();

int hundreds = num / 100;
int decade = (num - (hundreds * 100)) / 10;
int unit = num - (hundreds * 100) - (decade * 10);

int totalNum = hundreds + decade + unit;

System.out.print("三个位数相加的总数是:" + totalNum);


2.7 天数换算

Scanner scanner = new Scanner(System.in);

System.out.print("请输入总分钟数:");
int min = scanner.nextInt();

int day = (min / 60) / 24;
int year = day / 365;

day = day % 365;
System.out.print(min + "分钟是" + year + "年" + day + "天");


2.8 GMT偏移量

Scanner scanner = new Scanner(System.in);

System.out.print("请输入GMT偏移量:");
long offset = scanner.nextLong();

long totalTime = System.currentTimeMillis() + offset;
long totalSS = totalTime / 1000;
long ss = totalSS % 60;

long totalMM = totalSS / 60;
long mm = totalMM % 60;

long totalHH = totalMM / 60;
long hh = totalHH % 24;

String newSS = String.valueOf(ss);
String newMM = String.valueOf(mm);
String newHH = String.valueOf(hh);

if (ss < 10) {
newSS = "0" + newSS;
}
if (mm < 10) {
newMM = "0" + newMM;
}
if (hh < 10) {
newHH = "0" + newHH;
}

System.out.print("当前格林威治时间:" + newHH + ":" + newMM + ":" + newSS);

2.9 加速度计算

Scanner scanner = new Scanner(System.in);

System.out.print("请输初始速度:");
float v0 = scanner.nextFloat();

System.out.print("请输终止速度:");
float v1 = scanner.nextFloat();

System.out.print("请输经过时间:");
float t = scanner.nextFloat();

float a = (v1 - v0) / t;

System.out.print("平均加速度是:" + a);


2.13 利润计算器

Scanner scanner = new Scanner(System.in);

System.out.print("每月向银行存款的金额:");
double money = scanner.nextDouble();

System.out.print("请输入年利率:");
double interest = scanner.nextDouble();

System.out.print("请输入经过月份:");
int month = scanner.nextInt();

double totalMoney = 0;
for (int i = 1; i <= month; i++) {
totalMoney = (money + totalMoney) * (1 + (interest / 100) / 12);
}
System.out.print("在年利率为" + interest + "%的前提下,每月存" + money
+ "元,在经过" + month + "月后的总金额为:" + totalMoney + "元");


2.18 打印表格

int a = 1;
int b = 2;
System.out.println("a   b   pow(a,b)");
for (int i = 1; i <= 5; i++) {
System.out.println(a + "   " + b + "   " + (int)Math.pow(a, b));
a++;
b++;
}


2.20 利息额计算

Scanner scanner = new Scanner(System.in);

System.out.print("请输入收支金额:");
int balance = scanner.nextInt();

System.out.print("请输入年利率:");
double interestRate = scanner.nextDouble();

double interest = balance * (interestRate / 1200);
interest = (double) Math.round(interest * 100000) / 100000;

System.out.print("下个月需支付的利息额为:" +interest);


2.23 旅程费用计算

Scanner scanner = new Scanner(System.in);

System.out.print("请输入行驶距离:");
double distance = scanner.nextDouble();

System.out.print("每加仑多少英里:");
double miles = scanner.nextDouble();

System.out.print("每加仑的价格:");
double price = scanner.nextDouble();

double cost = (distance / miles) * price;
cost = (double) Math.round(cost * 100) / 100;

System.out.print("旅程的总花费为:" + cost + "元");


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: