我的问题是循环和异常。如果我运行这个程序,它将把我带到第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!");
}
}
}发布于 2016-03-18 01:03:25
一些建议:
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);
}发布于 2016-03-18 01:05:43
你要找的是所谓的“间断”。查看关于标签中断的信息:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html为您的外部while循环使用标签,例如point1 :,并在您希望从第1点重新启动的地方使用break point1;
https://stackoverflow.com/questions/36074273
复制相似问题