Presentation is loading. Please wait.

Presentation is loading. Please wait.

實作輔導 3 日期: 4/14(星期六) 09:10~12:00、13:10~16:00

Similar presentations


Presentation on theme: "實作輔導 3 日期: 4/14(星期六) 09:10~12:00、13:10~16:00"— Presentation transcript:

1 實作輔導 3 日期: 4/14(星期六) 09:10~12:00、13:10~16:00
地點:臺北市立大學 臺北市中正區愛國西路一號 (中正紀念堂站7 號出口) 公誠樓三樓 G316 電腦教室(資訊科學系) 可自行攜帶筆電 目標:協助習題、安裝java 環境、path設定 參加者:請 或直接到輔導地點 下次預定:

2 搶答!!

3 在分支敘述下變數內容追蹤(1) 印出結果? int a = 10, b = 8, c; If(a >= b) c = a+b; else c = a-b; System.out.print(“c = ” + c); 10 8 a b c RAM

4 在分支敘述下變數內容追蹤(2) 印出結果? int a = 10, b = 8, c; If(a >= b) c = a+b; c = c-1; System.out.print(“c = ” + c); 10 8 a b c RAM

5 在分支敘述下變數內容追蹤(3) 印出結果? int x = 10, y = 30, z = 40; If !(y == z) x = (z+1)%15 System.out.print(“x = ” + x); 10 30 40 x y z RAM

6 在分支敘述下變數內容追蹤(4) 印出結果? int p = 30, q = 20, r = 15; If(p >= 25 && q<50) r = p/r; else r = p-r; System.out.print(“r = ” + r); 30 20 15 p q r RAM

7 在分支敘述下變數內容追蹤(5) 印出結果? int p = 30, q = 20, r = 15; If (p >= 25 || q<50) r = p/r; else r = p-r; System.out.print(“r = ” + r); 30 20 15 p q r RAM

8 Q6 : 印出結果? public class loop_1 {
public static void main(String[] args) { int a = 20; int b = 15; int i=0; while (a>b) { i=i+1; a--; }//while System.out.println("i="+i+" a="+a); }//main }//class

9 while迴圈 II

10 追蹤迴圈 條件 S1; S2; S3; True False 無窮迴圈 空迴圈 如何跳出迴圈

11 追蹤迴圈1-1 條件 S1; S2; S3; True False 無窮迴圈? 空迴圈?

12 追蹤迴圈1-2 j=0; a=10; 條件 S1; S2; S3; True False 無窮迴圈? 空迴圈?

13 追蹤迴圈1 條件 S1; S2; S3; True False

14 追蹤迴圈2 條件 S1; S2; S3; True False true: 邏輯常數 “真” false:邏輯常數 “偽”

15 追蹤迴圈3 條件 S1; S2; S3; if (i>=500) break; //跳出迴圈
True False if (i>=500) break; //跳出迴圈 System.exit(-1); //跳出整個程式

16 追蹤迴圈4 S1; 條件 False True S2; break S3;

17 追蹤迴圈5 條件 S1; S2; S3; public class loop_1e {
public static void main(String[] args) { int a = 10; int b = 20; System.out.print("before loop: a="+a+" ,"); System.out.println((b>a)); while (b>a) { a++; System.out.print("inside loop:a="+a+" ,"); }//while System.out.print("after loop:a="+a+" ,"); }//main }//class 條件 False True S2; break S3;

18 迴圈結束方式二

19 String常用函數 比較字串是否相等 將字串轉換為大寫(UpperCase) 將字串轉換為小寫(LowerCase)
import java.util.Scanner; public class case_1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("輸入字串A:"); String A = input.nextLine(); System.out.print("輸入字串B:"); String B = input.nextLine(); System.out.print("A:" + A + "\nB:" + B + "\n\n"); System.out.print(“A equals to B ? ” + A.equals(B) + "\n" ); System.out.print("將A轉換為大寫、B轉換為小寫\n"); A = A.toUpperCase(); B = B.toLowerCase(); System.out.print("A equals to B ? " + A.equals(B) ); }//main } //class 比較字串是否相等 equals :字串物件之函數 A.equals(B) :字串A是否相等字串B true 或 false 將字串轉換為大寫(UpperCase) A.toUpperCase() 將字串轉換為小寫(LowerCase) =B.toLowerCase()

20 迴圈加入計算BMI

21 Example: 亂數加法練習 (個位數) (1)出10題個位數加法測驗(一次一題),不管對錯都出下一題, 直到答對為止; (2)出10題,答錯不出下一題,直到答對為止; (3)出N 題,由user決定題數,每題10分,答錯之題目須於結束時顯示; 目標: 亂數函數運用、判斷敘述、迴圈

22 Example: 亂數加法練習 (個位數) 二
答錯不出下一題,直到答對為止 出10題 顯示題次: 如第3題 計分: 第二次答對,得10-1分 第3次答對,得10-2分

23 只出一題 物件、變數宣告;初值設定 隨機出題 答題 答對? True S3; S2;

24 public class add_drill_2 { public static void main(String[] args) { SecureRandom sr = new SecureRandom(); Scanner input = new Scanner(System.in); int n1=0,n2= 0; int ans= 0, score=0, i=1; while (i<=10) { n1 = sr.nextInt(10); n2 = sr.nextInt(10); System.out.print(""+n1+"+"+n2+"=");//present question ans = input.nextInt(); if (ans== n1+n2) { score=score+10; System.out.println("答對,GREAT!!");} else System.out.println("答錯,加油! "); }//while }//main }//class What’s wrong?

25 public class add_drill_2 { public static void main(String[] args) { SecureRandom sr = new SecureRandom(); Scanner input = new Scanner(System.in); int n1=0,n2= 0; int ans= 0, score=0, i=1; while (i<=10) { //i=i+1; n1 = sr.nextInt(10); n2 = sr.nextInt(10); System.out.print(""+n1+"+"+n2+"=");//present question ans = input.nextInt(); if (ans== n1+n2) { score=score+10; System.out.println("答對,GREAT!! 分數:"+score+"分.");} else System.out.println("答錯,加油! 分數:"+score+"分."); i=i+1; }//while }//main }//class 不管對錯都出下一題,直到答10題為止

26 Where is the bug? How to revise?

27 答錯不出下一題,直到答對為止 第二次答對,得10-1分 第3次答對,得10-2分

28 第八周習題:亂數減法、除法練習 亂數減法、除法練習出n題 : (1)出10題,答錯不出下一題,直到答對為止; (2)出N題,由user決定題數,每題10分,答錯之題目須 於結束時顯示; 十位數減法出5題:不可小減大,每題10分 十位數除以個位數出5題:只能整除或需輸入商和餘數,每題10分 練習結束可再繼續,分數重算 可以加入:答錯不出下一題,直到答對為止 只完成減法、除法之一,也可繳交 繳交設計歷程檔及.java

29 再談分支/選擇結構

30 成績相對應的等第判定 輸入分數 利用if …else if…else 判斷並印出相對應的等第 或 switch case

31 True:真 False:偽 多重分支(multi-way if) 輸入分數 100<=分數<=90 False yes
90<分數<=80 印“優” True False 80<分數<=70 印“甲” 印“丙” True 印“乙”

32 True:真 False:偽 多重分支(multi-way if) 輸入分數 100<=分數<=90 False yes
分數<=80 印“優” True False 分數<=70 印“甲” 印“丙” True 印“乙”

33 Single statement 去除{ }

34 switch case 成績判定

35 多重選擇另一種形式: Switch case
Default: 預設 True false 與常數1值成立時 執行的敘述; case 符合常數1 break switch小括弧內為一運算式,計算出常數值。若與其後 case 的常數值相 符,就會執行該 case 的陳述。 switch (變數名稱或運算式) { case 符合常數1或字元1: 與常數1值成立時執行的敘述; ;//結束執行switch判斷; case 常數2或字元2 : 與常數2值成立時執行的敘述; break;//結束執行switch判斷; default: 與上述case常數值均不成立時執行的敘述; } 與常數2值成立時 執行的敘述; case 符合常數2 break 與常數3值成立時 執行的敘述; break

36 主題:成績判定 – switch case 100~90優 / 89~80甲 / 79~70乙 / 69~60丙 / 59~50丁 / 49~40 戊 / 39~30己 / 29~20庚 / 19~10辛 / ~1壬 / 0癸 利用switch case判斷使用者輸入 的成績位於哪個區間,並印出 相對應的等第。 利用score/10的結果來判斷分數 屬於哪個case, 由於9~0除10結果均為0,但9~1 & 0屬不同等地,因此判斷後才 印出。 執行結果

37 10 9 8 True false

38 Debug Bug在何處? Syntax error? 請回答!! How to handle? Let’s compile & run

39 比較

40 Example 2: 輸入星期幾?得英文 case要按大小順序?


Download ppt "實作輔導 3 日期: 4/14(星期六) 09:10~12:00、13:10~16:00"

Similar presentations


Ads by Google