首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从java中的特定点重新启动

从java中的特定点重新启动
EN

Stack Overflow用户
提问于 2016-03-18 00:49:33
回答 2查看 137关注 0票数 0

我的问题是循环和异常。如果我运行这个程序,它将把我带到第1点,但它只让我输入一个字符串,然后就停止了。而我希望它继续下去,就像它最初那样。我做错了什么?

代码语言:javascript
复制
    while (true) {
        try {
    //From here I want to start everytime. Point 1
            System.out.println("Do you whish to deposit or withdraw money or end the transaction?");
            Scanner readerBankTransactions = new Scanner(System.in);
            String BankTransaction = readerBankTransactions.nextLine();

            if (BankTransaction.equalsIgnoreCase(Transactions.ENDTRANSACTION.toString())) {
                System.out.println("Thank you for using our service.");
                break; //The programma should terminate here

            } else {
                while (true) {
                    if (BankTransaction.equalsIgnoreCase(Transactions.DEPOSIT.toString())) {
                        System.out.println("How much do you whish to deposit?");
                        Scanner readerDeposit = new Scanner(System.in);
                        double deposit = readerDeposit.nextDouble();
                        rekening.deposit(deposit);
                        double balance = rekening.getBalance();
                        System.out.println("Your balance is now: " + balance);
                        readerDeposit.close();
                        break; //from here I want to start again at point 1.

                    } else if (BankTransaction.equalsIgnoreCase(Transactions.WITHDRAW.toString())) {
                        System.out.println("How much do you whish to withdraw?");
                        Scanner readerWithdraw = new Scanner(System.in);
                        double withdraw = readerWithdraw.nextDouble();
                        rekening.withdraw(withdraw);
                        double balance = rekening.getBalance();
                        System.out.println("Your balance is now: " + balance);
                        readerWithdraw.close();
                        break; //from here I want to start again at point 1.
                    } 
                    readerBankTransactions.close();
                    readerbankAccountNumber.close();
                }
            } continue;
        } catch (InputMismatchException | NumberFormatException exception1) {
            System.out.println("This is not what you should have put in");
        } catch (InsufficientFundsException exception2) {
            System.out.println("insufficientfunds!");
        } catch (MaximumWithdrawException exception3) {
            System.out.println("Maximum withdraw restriction!");
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-03-18 01:03:25

一些建议:

  • 尽量避免while (true)模式,然后是continuebreak语句。这使得逻辑很难理解。
  • 您应该在helper方法中隔离一些逻辑。同样,这将使主要逻辑更容易理解。
  • 我不明白内部while循环的目的。你在这里想做什么?

下面是我建议的重写:

代码语言:javascript
复制
do {
    try {
        String BankTransaction = getInitialSelection();

        if (isDepositRequest(BankTransaction)) {
            handleDeposit();
        } else if (isWithdrawalRequest(BankTransaction)) {
            handleWithdrawal();
        } 
    } catch (InputMismatchException | NumberFormatException exception1) {
        System.out.println("This is not what you should have put in");
    } catch (InsufficientFundsException exception2) {
        System.out.println("insufficientfunds!");
    } catch (MaximumWithdrawException exception3) {
        System.out.println("Maximum withdraw restriction!");
    }
} while (!isExitRequest(BankTransaction));
System.out.println("Thank you for using our service.");

这是假设handleDeposit()的定义,handleWithdrawal()与原始代码中相应的代码匹配。

另外,我假设了以下帮助方法:

代码语言:javascript
复制
private boolean isDepositRequest(String bankTransaction) {
    return Transactions.DEPOSIT.toString().equalsIgnoreCase(bankTransaction);
}

private boolean isWithdrawalRequest(String bankTransaction) {
    return Transactions.WITHDRAW.toString().equalsIgnoreCase(bankTransaction);
}

private boolean isExitRequest(String bankTransaction) {
    return Transactions.ENDTRANSACTION.toString().equalsIgnoreCase(bankTransaction);
}
票数 0
EN

Stack Overflow用户

发布于 2016-03-18 01:05:43

你要找的是所谓的“间断”。查看关于标签中断的信息:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html为您的外部while循环使用标签,例如point1 :,并在您希望从第1点重新启动的地方使用break point1;

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36074273

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档