本帖最後由 tonyh 於 2012-11-24 17:32 編輯
複習 continue 與 break 語法, 改寫我們剛剛寫的程式,
讓使用者可反覆地查詢, 並在輸入'end' 後結束程式.- import java.io.Console;
- public class ch43
- {
- public static void main(String args[])
- {
- String name;
- String tel[][]={{"大雄","0980-454666"},
- {"技安","0911-333898"},
- {"宜靜","0922-999579"},
- {"阿福","0938-234567"},
- {"小叮噹","0911-864356"}};
- Console console=System.console();
- System.out.println("***小米的電話簿***");
- while(true)
- {
- int sum=0;
- System.out.println("\n1.輸入'all'顯示完整電話簿\n2.輸入姓名查詢目標對象的電話\n3.輸入'end'結束程式");
- System.out.print("請在此輸入: ");
- name=console.readLine();
- if(name.equals("all")==true)
- {
- System.out.println("編號\t姓名\t電話");
- System.out.println("============================");
- for(int i=0; i<=4; i++)
- {
- System.out.print((i+1)+"\t");
- for(int j=0; j<=1;j++)
- {
- System.out.print(tel[i][j]+"\t");
- }
- System.out.println();
- }
- continue;
- }else if(name.equals("end")==true)
- {
- break;
- }
- else
- {
- for(int i=0; i<=4; i++)
- {
- if(name.equals(tel[i][0])==true)
- {
- System.out.println(tel[i][0]+"的電話是"+tel[i][1]);
- sum++;
- }
- }
- if(sum==0)
- {
- System.out.println("查無此人!");
- }
- continue;
- }
- }
- }
- }
複製代碼 |