为什么我的break语句会突破整个程序?

时间:2012-11-17 01:39:33

标签: java loops try-catch break

如果我的JOptionPane输入不正确,我的Exception子句中的break;语句会停止整个程序,它会在catch块之后执行我的操作,为什么会这样?

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);

    }
}

4 个答案:

答案 0 :(得分:3)

因为使用break你跳出循环(这是程序的结束),如果你想在catch块部分之后执行,只需删除break;


查看

答案 1 :(得分:1)

我怀疑您希望continue,而不是break。看到这里的区别:

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

continue告诉循环跳过循环体中的所有以下语句并返回到循环体的顶部,重新检查条件,然后从那里继续正常循环。

break告诉循环立即结束,忽略循环体中的任何进一步指令。

答案 2 :(得分:0)

break从封闭循环中逃脱,在你的情况下意味着主要的结束......

答案 3 :(得分:0)

break离开循环,你的main方法循环后没有任何东西。如果您想继续循环播放,请将break;替换为continue;