簡體   English   中英

為什么我的break語句脫離整個程序?

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

break; 如果我對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);

    }
}

因為使用break會跳出循環(這就是程序的結尾),因此,如果要在catch塊部分之后執行,只需刪除break;


看到

我懷疑你希望continue ,而不是break 在這里看到區別:

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

continue告訴循環跳過循環主體中的以下所有語句,並返回到循環主體的頂部,重新檢查條件,然后從那里繼續正常循環。

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

break從封閉循環中逃脫,在您的情況下,這意味着main ...

您正在break循環,您的main方法中的循環之后什么也沒剩下。 如果要繼續循環,請更換break; continue;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM