我的问题是关于循环和异常。如果我运行这个程序,它将带我到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!");
}
}
}
答案 0 :(得分:0)
一些建议:
while (true)
后跟continue
或break
语句的模式。这使逻辑非常难以理解。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);
}
答案 1 :(得分:0)
您正在寻找的是被标记的休息时间。在https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html查看带标签的休息时间的信息
为例如point1 :
的外部while循环使用标签,并在要从第1点重新启动的位置使用break point1;