2012-11-17 34 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

相关问题