Presentation is loading. Please wait.

Presentation is loading. Please wait.

實作輔導 4 日期: 4/21(星期六) 13:10~16:00、13:10~16:00

Similar presentations


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

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

2 搶答!!

3 Q1 : 印出結果? int n = 7, i=0; while (i<n) { System.out.print(i); i++;} System.out.println(", i="+i);

4 Q2 : 印出結果? n = 7; i=0;//已宣告 while (i<n) { i++; System.out.print(i); } System.out.println(" ,i="+i);

5 Q3 : 印出結果? n = 7; i=0; while (i<n) { n--; System.out.print(i); } System.out.println(" ,i="+i);

6 Q4 : 印出結果? int a, b; a=b=11; i=0; while (a>=b) { ++i; a--; }//while System.out.println("i="+i+" a="+a);

7 Q5 : 印出結果? a=b=13; i=0; while (a>5 && a<9) { ++i; a--; }//while System.out.println("i="+i+" a="+a);

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

9 自我練習 int a, b; a=b=11; i=0; while (a>=b) { ++i; a--; }//while
System.out.println("i="+i+" a="+a); a=b=13; while (a>5 && a<9) { }//main }//class public class challenge { public static void main(String[] args) { int n = 7, i=0; while (i<n) { System.out.print(i); i++;} System.out.println(", i="+i); n = 7; i=0;//已宣告 i++; } System.out.println(" ,i="+i); n = 7; i=0; n--;

10 臺北市立大學 資訊科學系(含碩士班) 賴阿福
迴圈: for, do….while 臺北市立大學 資訊科學系(含碩士班) 賴阿福

11 for迴圈 條件 S1; 初值設定; S3; S1; For(初值設定;條件;遞增或遞減) { s2; } S3; S2;
True False 初值設定; 遞增或遞減 S1; For(初值設定;條件;遞增或遞減) { s2; } S3; For loop body

12 for迴圈 i<=n S1; S2; S3; True False i=1; ++i; S1; For(初值設定;條件;遞增或遞減) { s2; } S3; For loop body n = input.nextInt(); for(i=1;i<=n;++i) { System.out.println(“i="+i); }

13 for迴圈:印1~n 條件 S1; 初值設定; 不同形式 S3;
{ s2; s3; } S4; 條件 S1; S2; S3; True False 初值設定; 遞增或遞減 n = input.nextInt(); for(i=1;i<=n;++i) { System.out.println(“i="+i); } //印出多少?? n = input.nextInt(); i=1; for(;i<=n;) { System.out.println(“i="+i); ++i; //i=i+1; } System.out.println(“i="+i); //印出多少?? 不同形式

14 for迴圈:印n~1 條件 S1; 初值設定; S3;
{ s2; s3; } S4; 條件 S1; S2; S3; True False 初值設定; 遞增或遞減 n = input.nextInt(); for(i=n;i>=1;--i) { System.out.println(“i="+i); } //印出多少?? n = input.nextInt(); i=n; for(;i>=1;) { System.out.println(“i="+i); --i; //i=i-1; } System.out.println(“i="+i); //印出多少??

15 Do.. While ();迴圈 S1; do { s2; } while (條件); S3; 條件 S1; S3; S2; True
Do while loop body False S3;

16 Do.. While ();迴圈 印1~10 S1; S1; do { s2; } while (條件); S3; S2; 條件 i=1; do { System.out.println(“i="+i); i++; } while (i<=10) ; System.out.println(“i=”+i); //印出多少?? True False S3;

17 Do.. While ();迴圈 印1~n S1; S1; do { s2; } while (條件); S3; S2; 條件 n = input.nextInt(); i=1; do { System.out.println(“i="+i); i++; } while (i<=n) ; System.out.println(“i="+i); //印出多少?? True False S3;

18 Do.. While ();迴圈 印1~10 S1; S1; do { s2; } while (條件); S3; S2; 條件 i=0; do { i++; System.out.println(“i="+i); } while (i<10) ; System.out.println(“i="+i); //印出多少?? True False S3;

19 Do.. While ();迴圈 印1~n S1; S1; do { s2; } while (條件); S3; S2; 條件 n = input.nextInt(); i=0; do { i++; System.out.println(“i="+i); } while (i<n) ; System.out.println(“i="+i); //印出多少?? True False S3;

20 比較三種迴圈 Trace all_loop_0.java 印1~n

21 運用三種迴圈解 S=1+2+….+n

22 For迴圈與while迴圈對等: S=1+2+….+n
n = input.nextInt(); s=0; i=0; while (i<=n) { s=s+i; i++;} System.out.println(" "+n+"="+s); n = input.nextInt(); s= 0; For(i=1;i<=n; i++ ) s=s+i; System.out.println(" "+n+"="+s); n = input.nextInt(); s= 0; i=1; for(;i<=n;) { s=s+i; i++; } System.out.println(" "+n+"="+s); 完成對等 equivalence

23 For迴圈之不同形式 n = input.nextInt();
s= 0; i=1; for(;i<=n; s=s+i, i++ ) ; System.out.println(" "+n+"="+s ); n = input.nextInt(); s= 0; For(i=1;i<=n; i++ )// ++i s=s+i; System.out.println(" "+n+"="+s); n = input.nextInt(); s= 0; i=1; for(;i<=n;s=s+i++) ; System.out.println(" "+n+"="+s ); n = input.nextInt(); s= 0; i=1; for(;i<=n;) { s=s+i; i++; } System.out.println(" "+n+"="+s);

24 遞增或遞減 i=i+1; ++i; i++; i+=1; s=s+i++; s=s+(++i); i=i-1; --i; i--;
完成對等??

25 比較遞增: s=s+i; i=i+1; s=s+i; i=1; s=10; s=s+(++i); i=i+1; i=1; s=10;
System.out.println(s); System.out.println(i); 11 2 i=i+1; s=s+i; i=1; s=10; s=s+(++i); System.out.println(s); System.out.println(i); 12 2

26 比較遞減 搶答 搶答 i=1; s=10; s=s+(--i); i=1; s=10; s=s+i--;
System.out.println(s); ? 搶答 i=1; s=10; s=s+(--i); System.out.println(s); 搶答 ?

27 Do.. While ();迴圈 S=1+2+….+n 條件 S1; S3;
n = input.nextInt(); i=1; s=0; do { s=s+i; i++; }while (i<=n); System.out.println(" "+n+"="+s); True False S3;

28 Debug :輸入奇數n,求S=1+3+5+.........+n 搶答: 那些錯誤?如何修? 寫出編號及修改結果
import java.util.Scanner; public class loop_debug_1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n=7,i=0, s=0; System.out.println("輸入奇數n,求S= n\n"); while (n>=1) { System.out.print("輸入奇數(-1:end):"); n = input.nextInt(); for(i=1;i<=n;++i) s=s+i; System.out.println(" "+n+"="+s); }//while }//main }//class 搶答: 那些錯誤?如何修? 寫出編號及修改結果

29 Debug :輸入奇數n,求S=1+3+5+.........+n 處理輸入錯誤 import java.util.Scanner;
public class loop_debug_1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n=7,i=0, s=0; System.out.println("輸入奇數n,求S= n\n"); while (n>=1) { System.out.print("輸入奇數(-1:end):"); n = input.nextInt(); if (n%2==0) { System.out.println("輸入錯誤,須為奇數!"); continue;} for(i=1;i<=n;++i) s=s+i; System.out.println(" "+n+"="+s); }//while }//main }//class 處理輸入錯誤

30 追蹤for loop int n=7; for(i=1;i<=n;++i) { s=s+i; ++i; }
System.out.println(“s"="+s); 搶答:

31 Debug :輸入整數n,求S= 1*2*3*.......*n 搶答: 那些錯誤?如何修? 寫出編號及修改結果
import java.util.Scanner; public class all_loop_1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n=7,i=0; int s=0; System.out.println("輸入整數n,求S=1*2*3* *n\n"); while (n>=1) { System.out.print("輸入整數(-1:end):"); n = input.nextInt(); for(i=1;i<=n;++i) s=s*i; System.out.println("1*2*3*...*"+n+"="+s); }//while }//main }//class 搶答: 那些錯誤?如何修? 寫出編號及修改結果

32 第9周習題: 共二題(全部要完成) 9-1: 三種迴圈解s=1*2+3*4+……..+n*(n+1)
輸入n(奇數), 求s=1*2+3*4+5*6+……..+n*(n+1) 輸入錯誤(如4),要求重新輸入 輸入n後,分別以while, do..while, for 等三種迴圈求s 放置於同一.java 使用者可重複輸入,直到輸入<=0或回答N,才結束程式題,答錯不出下一題,直到答對為止; (2)出N題,由user決定題 數,每題10分,答錯之題目須於結束時顯示; 繳交”設計歷程”檔及.java 9-2:三種迴圈解s=1*2+2*3+……..+n*(n+1) 輸入n, 求s=1*2+2*3+3*4+……..+n*(n+1)

33 Review switch case

34 改為5等第

35 改變運算式 (score-50)/10

36 主題:字元金字塔 - 斜金字塔 利用迴圈印出 「 * 」,逐行增 加印出個數,直到印出7層斜 金字塔。 本題利用到巢狀迴圈的概念
巢狀迴圈為迴圈範圍內又有迴 圈,從外層來看,內層迴圈只 屬與外層迴圈內的動作。因此 外層迴作用,內層迴圈開始運 作到執行結束後,又回到外層 迴圈。 執行結果


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

Similar presentations


Ads by Google