在java中从某个点重新启动

在java中从某个点重新启动,java,Java,我的问题涉及循环和异常。如果我运行这个程序,它将把我带到第1点,但它只让我输入一个字符串,然后停止。然而,我希望它继续下去,就像它最初做的那样。我做错了什么 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 tr

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

    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!");
        }
    }
}
一些建议:

  • 尽量避免出现
    while(true)
    后接
    continue
    break
    语句的模式。这使得逻辑很难遵循
  • 您应该在helper方法中隔离一些逻辑。同样,这将使主要逻辑更容易遵循
  • 我不明白内部
    while
    循环的用途。你想在这里完成什么
下面是我建议的改写:

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()
的定义与原始代码中的相应代码相匹配

此外,我还假设了以下帮助器方法:

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

你要找的是所谓的休息。在以下位置查看有关标签中断的信息:
为外部while循环使用标签,例如
point1:
,并在要从点1重新启动的位置使用
break point1

如果您能更详细地说明您的问题所在以及您正试图实现的目标,那将是一件非常棒的事情。您还可以查看以改进问题。欢迎来到SO!一般来说……只是糟糕的代码,没有意义。所以,要么在你的问题和目标上非常清楚,要么……祝你好运。你知道它停在哪里吗?如果没有,请使用打印语句或功能类似的语句来确定问题所在。这是nextLine()语句吗?非常感谢您的反馈!昨天我继续做这个程序,但最后我得到了太多的while和if语句,以至于我完全失去了整体画面。所以我重新开始使用switch语句,它工作得更好。这似乎是一个更好的解决方案。我试图使用它,但它在while循环中给了我一个错误。它表明我还没有声明一个名为BankTransaction的变量。我怀疑这是因为我在try语句中初始化了I?我不知道怎么解决这个问题谢谢!这在重新创建switch语句的程序时帮助很大。