1. 程式人生 > >Java語言程式設計(基礎篇)(原書第10版) 練習題答案 第2章

Java語言程式設計(基礎篇)(原書第10版) 練習題答案 第2章

基礎篇課後習題答案,做了大部分,雖然不一定是最佳程式碼,但是保證每個都能執行,如有更好的答案,歡迎討論

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 + "元");