Java語言程序設計(第三版)第二章課后習題答案(僅供參考)


2.1   注意不同類型轉換

1 import java.util.Scanner;
2 
3 public class Ch02 {
4     public static void main(String[] args) {
5         Scanner sc = new Scanner(System.in);
6         double f = sc.nextDouble();
7         double t = (5.0/9)*(f-32);   //  注意 (5/9) 結果為 整形 要寫成 (5.0/9)
8         System.out.println(t)
9 }

 

2.2

 1 import java.util.Scanner;
 2 
 3 public class Ch02 {
 4     public static void main(String[] args) {
 5         Scanner sc = new Scanner(System.in);
 6         System.out.println("請輸入圓柱體半徑和高:");
 7         double r = sc.nextDouble();
 8         double h = sc.nextDouble();
 9         double v = Math.PI * r * r * h;  //圓周率方法  Math.PI
10         System.out.println(v);
11 }

 

2.3

 1 import java.util.Scanner;
 2 
 3 public class Ch02 {
 4     public static void main(String[] args) {
 5         Scanner sc = new Scanner(System.in);
 6         System.out.println("請輸入體重和身高:");
 7         double g = sc.nextDouble();
 8         double h = sc.nextDouble();
 9         double BMI = g / (h * h);
10         System.out.println(BMI);
11 }

 

2.4  使用 循環 每次對 個位 取數相加 取數后 除以10 使前一位 變為 個位 繼續 判斷小於等於0時停止

 1 import java.util.Scanner;
 2 
 3 public class Ch02 {
 4     public static void main(String[] args) {
 5         int sum = 0;
 6         Scanner sc = new Scanner(System.in);
 7         int num1 = sc.nextInt();
 8         while (num1 > 0)        // 與 0 比較 判斷 是否 需要 取數
 9         {
10             sum += (num1%10);   // 通過取余  獲得個位數字
11             num1 /= 10;         // 每次取余后 除以10 
12         }
13         System.out.println(sum);
14 }

 

2.5  注意 System.currentTimeMillis()方法 返回 long 形 需要轉換為 int

 1 public class Ch02 {
 2     public static void main(String[] args) {
 3         // 通過 System.currentTimeMillis() 方法 獲得 從1970年1月1日 00:00:00 到現在的 毫秒數
 4         // 28800000 是 格林時間 與 我們 時區 的 時間差值
 5         // 對 86400000 取余 是 把 滿一天的時間都去掉 獲取多出來的不足一天的時間
 6         int t = (int)((System.currentTimeMillis()+28800000)%86400000);
 7         int hour = t/3600000;               // 除 3600000 獲取滿小時的個數 即 求小時 為幾點
 8         int mine = t%3600000/60000;         // 計算 不足一小時 的時間 里 有多少分鍾
 9         int s = t%3600000%60000/1000;        // 計算不足一分鍾的時間里 有多少秒  不要忘記 除以 1000 (因為 單位 為 毫秒)
10         System.out.println("當前時間: "+hour+":"+mine+":"+s+"  GMT");
11 }

 

2.6  a 不能為 0 

  b2 - 4 * a * c  不能為 0

 1 import java.util.Scanner;
 2 
 3 public class Ch02 {
 4     public static void main(String[] args) {
 5         double sum,a,b,c,t;
 6         Scanner sc = new Scanner(System.in);
 7         while (true) {
 8             a = sc.nextDouble();
 9             b = sc.nextDouble();
10             c = sc.nextDouble();
11             t = b*b-4*a*c;
12             if (a == 0) {
13                 System.out.println("a 不能為 0,請重新測試 ^_^");
14             } else if (t < 0) {
15                 System.out.println("b*b-4*a*c不能為0,請重新測試");
16             }
17             else
18             {
19                 break;
20             }
21         }
22         sum = ((-b+Math.sqrt(t)/(2*a)));     //( 2*a )  注意加括號 
23         System.out.println(sum);

 

2.7  注意計算公式先后順序  多使用小括號

 1 import java.util.Scanner;
 2 
 3 public class Ch02 {
 4     public static void main(String[] args) {
 5         Scanner sc = new Scanner(System.in);
 6         double yrate = sc.nextDouble();
 7         double amount = sc.nextDouble();
 8         double year = sc.nextDouble();
 9         double month = (amount * (yrate/12))/(1-(1/Math.pow(1+yrate/12,year*12)));
10         double sumamount = month*12*year;
11         System.out.println("月支付金額:"+month+"\n總償還金額:"+sumamount);
12 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM