Java 程式設計 標準輸出入與流程控制
本章大綱 標準輸出入 (Standard I/O) 分支 (Branch) if ~ else switch ~ case 迴圈 (Loop) for while do ~ while 中斷指令 break continue 總整理
標準輸出 定義 : 將資料印到螢幕上 Java 標準輸出指令 System.out.println( 資料 ) 將資料印出後換行 System.out.print( 資料 ) 將資料印完後不換行
標準輸入 法一 : 由命令列傳入參數值 (Command Line Argument) 做法 :java TestArg I am happy
標準輸入範例 public class ArgumentInput { public static void main(string[] args) { System.out.println(args[0]); System.out.println(args[1]); System.out.println(args[2]); java ArgumentInput I am happy args[2] args[1] args[0]
標準輸入 法二 : 使用 Stream I/O 太過複雜, 後面將有一個章節專門講述
if ~ else if ( 條件 ) { // 如果條件為真, 就做這塊指令 1 指令 2 else { // 如果條件為假, 就做這塊指令 1 指令 2 若所包住的指令只有一行, 則括號可以不用寫
if ~ else 練習題 請製作一個傳入分數, 能夠印出 Grade A ~ F 的 Java Application java Grading 85 印出 Grade B 提示 : 放在 Grading 後面的 85, 會由 main 函式的 String[] args 部份接收 您可以用 args[0] 抓到這個 85 由於 args 宣告成字串, 所以 85 是文字的 85, 不是數字的 85 必須經過下列轉換後才能變成數字 int Score = Integer.parseInt(args[0]);
if ~ else 練習題 public class Grading { public static void main (String[] args) { int Score = Integer.parseInt(args[0]); if (Score >= 90) System.out.println ("Grade A"); else if (Score >= 80) System.out.println ("Grade B"); else if (Score >= 70) System.out.println ("Grade C"); else if (Score >= 60) System.out.println ("Grade D"); else System.out.println ("Grade F");
switch ~ case switch ( 陳述式 ) { case 文數字 : 指令 ; break; case 文數字 : 指令 ; break; case 文數字 : 指令 ; break; default: 指令 ; break;
switch ~ case case 文數字 : case 後方只能接 byte / short / int / char 四種型態 如 :case 1: 或 case a : 不像 Visual Basic 的 select case, 可以填入一個範圍 此乃 Java switch ~ case 之限制 break; Break 指令可以阻止程式繼續往下執行, 並跳出整個 switch ~ case 結構 但有時您會希望程式可以繼續往下執行
switch ~ case 練習題 以下程式可以用來計算某月有幾天 : public class GetMonthDays { public static void main (String[] args) { int Year = Integer.parseInt(args[0]); int Month = Integer.parseInt(args[1]); switch (Month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: System.out.println("31 days"); break;
switch ~ case 練習題 case 4: case 6: case 9: case 11: System.out.println("30 days"); break; case 2: if ( ((Year%4 == 0) && (Year%100!= 0)) (Year%400 ==0) ) System.out.println("29 days"); else System.out.println("28 days"); break;
switch ~ case 練習題 執行 : java GetMonthDays 2001 7 args[0] args[1] 結果 31 days
for 格式 : for ( 起始條件 ; 執行條件 ; 遞增值 ) { 指令 ; 範例 : public class ForExample1 { public static void main (String[] args) { int i; for (i=1; i <=10; i++) System.out.println ("i = " + i);
for 當迴圈中止時 : public class ForExample2 { public static void main (String[] args) { int i; for (i=1; i <=10; i++) System.out.println ("i = " + i); System.out.println ("After loop, i = " + i);
for 起始條件, 執行條件, 遞增值內部不一定只能擺一個變數 : public class ForExample3 { public static void main (String[] args) { for (int i=0, j=20; i<=20 && j>=1; i+=2, j-=2) System.out.println("i = " + i + " j = " + j);
while 格式 : while ( 條件 ) { 指令 ; 範例 : public class WhileExample { public static void main (String[] args) { int i=1; while (i++<=10) System.out.println("i = " + i); 適用場合 : 不知道幾次迴圈才會停的時候
do ~ while 格式 : do { 指令 ; while ( 條件 ); 範例 : 想計算所有平方值小於 100 的數字, 並將該數字及其平方值印出來
do ~ while public class DoWhileExample { public static void main (String[] args) { int i=0, j; do { i++; j = i * i; System.out.println ( i + " * " + i + " = " + j); while (j < 100);
break 用途 中斷目前區塊 (block) 的流程 格式 break [ 跳躍標籤 ] 其中 break + 跳躍標籤的情況常用於一口氣要跳出兩層以上的巢狀迴圈
break 範例 1 以下範例會在內圈變數 j == 7 時停止, 而只印出 1~6 十次 public class BreakExample { public static void main(string[] args) { int i, j; for (i=1; i<=10; i++) { for (j=1; j<=10; j++) { if (j==7) { break; System.out.print("j = " + j + " "); System.out.println(""); System.out.println( Total Loop of i = " + (i-1));
break 範例 2 以下範例會在內圈變數 j == 7 時停止, 而只印出 1~6 一次 public class BreakExample { public static void main(string[] args) { int i, j; for (i=1; i<=10; i++) { for (j=1; j<=10; j++) { if (j==7) { break outer; System.out.print("j = " + j + " "); System.out.println(""); outer: System.out.println( Total Loop of i = " + (i-1));
continue 用途 可以跳過這一次的迴圈不執行, 直接從下一個迴圈開始執行 格式 continue [ 跳躍標籤 ];
continue 範例 以下範例會跳過內圈變數 j == 7 那一圈, 而印出 1~6, 8~10 十次 public class BreakExample { public static void main(string[] args) { int i, j; for (i=1; i<=10; i++) { for (j=1; j<=10; j++) { if (j==7) { continue; System.out.print("j = " + j + " "); System.out.println(""); System.out.println("Total Loop of i = " + (i-1));
總整理 Java 的標準輸出指令為何? Java 如何做到標準輸入? 有幾種方法? 請回憶下列指令的格式 : if ~ else switch ~ case for while do ~ while break 及 continue
Lab 5-1 Example Write a program that accepts two numbers from the command line and prints them out. Then use a for loop to print the next 13 numbers in the sequence where each number is the sum of the previous two. For example: input> java prob5-1 1 3 output> 1 3 4 7 11 18 29 47 76 123 322 521 843 1364
Lab 5-2 Modification (3%) Write a program that accepts two numbers from the command line and prints them out. Then use a for loop to print the next X numbers in the sequence where each number is the sum of the previous two. For example: X=13 input> java prob5-2 1 3 13 output> 1 3 4 7 11 18 29 47 76 123 322 521 843 1364
Lab 5-3 Extra Points (5%) 23. 請設計 Java 程式計算在指定年份時的年齡, 這是使用生日的 2 位數西元年份, 例如 :67 年或 78 年, 和 2 位數的指定年份時的年齡 ( 注意!Y2K 的問題 ), 例如 : 輸出結果的範例如下所示 : Email to george_hs@yahoo.com before next class (1 person only) 生日年份 : 67 指定年份 : 07 年齡 : 40
Reference http://home.cogeco.ca/~ve3ll/jatutor3.ht m