简体   繁体   English

为什么我的break语句脱离整个程序?

[英]why does my break statement breaks out of the entire program?

The break; break; statement in my Exception clause stops the entire program if my have improper input to the JOptionPane, it would not execute what I have after the catch block, why is that? 如果我对JOptionPane的输入不正确,则Exception子句中的语句会停止整个程序,它不会执行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);

    }
}

because using break you are jumping out of the loop (and that is the end of your program), if you wish to execute after catch block part, simply remove break; 因为使用break会跳出循环(这就是程序的结尾),因此,如果要在catch块部分之后执行,只需删除break;


See 看到

I suspect you wish to continue , not break . 我怀疑你希望continue ,而不是break See the difference here: 在这里看到区别:

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

continue tells the loops to skip all of the following statements in the loop body and return to the top of the loop body, recheck the condition, and then continue looping normally from there. continue告诉循环跳过循环主体中的以下所有语句,并返回到循环主体的顶部,重新检查条件,然后从那里继续正常循环。

break tells the loop to end immediately, ignoring any further instructions in the loop body. break告诉循环立即结束,而忽略循环主体中的任何进一步指令。

break从封闭循环中逃脱,在您的情况下,这意味着main ...

You're break ing out of the loop and there's nothing left after the loop in your main method. 您正在break循环,您的main方法中的循环之后什么也没剩下。 If you want to continue looping, replace the break; 如果要继续循环,请更换break; with continue; continue; .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM