2012-11-17 33 views
-1

我的Exception語句中的break;語句會停止整個程序,如果我的JOptionPane輸入不正確,它將不會執行catch塊後面的內容,爲什麼?爲什麼我的break語句突破了整個程序?

public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     Customer forrest = new Customer("Forrest Grump", 1, 
       "42 New Street, New York, New York"); 
     Customer random = new Customer("Random Name", 2, 
       "44 New Street, New York, New York"); 
     Customer customer[] = { null, forrest, random }; 
     int whichOption = 1; 
     int id = 0; 
     char action = ' '; 
     char accSrc = ' '; 
     char accDest = ' '; 
     double amount = 0; 
     BankAccount src = null; 
     do { 

      try{ 
      // process JOptionPane input information 
      String input = JOptionPane 
        .showInputDialog("Please enter your transaction information: "); 
      Scanner s = new Scanner(input); 
      id = Integer.parseInt(s.next()); 
      action = Character.toUpperCase((s.next().charAt(0))); 

      if (action == 'T') { 
       amount = s.nextDouble(); 
       accSrc = s.next().charAt(0); 
       accDest = s.next().charAt(0); 
      } else if (action == 'G' || action == 'I') { 
       accSrc = s.next().charAt(0); 
      } else { 
       // if D,W 
       amount = s.nextDouble(); 
       accSrc = s.next().charAt(0); 
      } 

      } catch (Exception e){ 
       System.out.println("Invalid input!"); 
       break; 
      } 
      // taking action accordingly (T)ransfer, (D)eposit, (W)ithdraw, (I)nterest 
      if (action == 'T') { 

       (customer[id].getAccount(accSrc)).transfer(amount, 
         customer[id].getAccount(accDest)); 
      } else if (action == 'G') { 
       System.out.println("The current balance on your " + accSrc 
         + " account is " 
         + customer[id].getAccount(accSrc).getBalance() + "\n"); 
      } else if (action == 'D') { 
       (customer[id].getAccount(accSrc)).deposit(amount); 
       //You have successfully depositted $xx.xx 
      } else if (action == 'W') { 
       (customer[id].getAccount(accSrc)).withdraw(amount); 
      } else if (action == 'I') { 
       (customer[id].getAccount(accSrc)).computeInterest(); 
      } 
      whichOption = JOptionPane 
        .showConfirmDialog(null , "Do you want to continue?"); 

      System.out.println("The balance on " + customer[id].getName() 
        + " auto account is " + customer[id].A.balance); 
      System.out.println("The balance on " + customer[id].getName() 
        + " savings account is " + customer[id].S.balance); 
      System.out.println("The balance on " + customer[id].getName() 
        + " checkings account is " + customer[id].C.balance); 
      System.out.println("The balance on " + customer[id].getName() 
        + " loan account is " + customer[id].L.balance + "\n"); 

     } while (whichOption == 0); 

    } 
} 

回答

1

我懷疑你想continue,而不是break。看到這裏的區別:

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html

continue告訴循環跳過下面的所有語句的循環體,並返回到循環體的頂部,再次檢查條件,然後從那裏繼續正常循環。

break指示循環立即結束,忽略循環體中的任何進一步指令。

+0

如果在異常塊中,我想重新啓動程序,而不是重新啓動循環,應該使用什麼代碼重新啓動'main()'? – user133466

+0

快速解決方法是用'this.main(args)替換'break;'; System.exit(0);'。你實際上只需要'main(args); System.exit(0);' –

+0

如果我們再次調用'main(args)'就不會成爲遞歸嗎?所以如果用戶提供無效輸入太多次,我們不會得到一個stackoverflow錯誤? – user133466

3

因爲使用break你跳出循環(這是你的程序的結束),如果你想後catch塊的一部分來執行,只需刪除break;


從封閉循環,而你的情況指的主要結束
+0

看起來像我意外停止了該程序,如果我想停止沒有循環的程序,我應該如何在異常塊中停止程序? – user133466

+0

取決於你想如何退出它,常用的方法是使用'System.exit()' –

+0

謝謝,它的工作原理!如果在異常塊中,我想重新啓動程序,我應該使用什麼代碼重新啓動'main()'? – user133466

0

break逃逸......

0

break荷蘭國際集團退出循環,並沒有什麼在你的main方法中的循環後離開。如果要繼續循環,請將break;替換爲continue;

+0

如果在異常塊中,我想重新啓動程序,而不是重新啓動循環,應該使用什麼代碼重新啓動'main()'? – user133466

+0

再次調用main ...用'main(args)替換'break;';' – StackOverflowed

+0

不會變成遞歸嗎?如果用戶提供的無效輸入次數太多,我們不會得到一個stackoverflow錯誤? – user133466

相關問題