提问人:Brian Gomez 提问时间:10/5/2022 最后编辑:Brian Gomez 更新时间:10/5/2022 访问量:73
ATM 机应用的 NoSuchElementException 错误(已修改)
NoSuchElementException error for ATM Machine app (Modified)
问:
我目前正在学习 Java,我正在尝试通过构建 ATM 机应用程序来保留我学到的信息(我计划在未来添加更多内容)。 我目前的问题是我想反复询问用户“您想做什么”,直到提供有效输入(当前有效输入是“提款”和“存款”)。
我有一个循环,如果输入无效,它会反复询问用户“请选择一个有效的选项”。如果输入有效,它将仅执行一次,询问“您想做什么”,然后显示 NoSuchElementException。不知道如何解决这个问题。
这是我的代码:
App.java
import java.util.Scanner;
public class App {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
boolean done = false;
while(!done) {
System.out.println("What woul you like to do?");
String response = scanner.next().toLowerCase();
Transactions newTransaction = new Transactions();
if (response.equals("withdraw")) {
newTransaction.Withdrawal();
} else if (response.equals("deposit")) {
newTransaction.Deposit();
} else {
System.out.println("Please select a valid choice");
}
}
scanner.close();
}
}
Transactions.java
import java.util.Scanner;
public class Transactions {
private int currentBalance = 100;
public void Withdrawal() {
Scanner scanner = new Scanner(System.in);
System.out.println("How much would you like to withdraw?");
int withdrawAmount = scanner.nextInt();
if (withdrawAmount > 0) {
if (currentBalance > 0) {
currentBalance -= withdrawAmount;
System.out.println("Amount withdrawn: $" + withdrawAmount);
System.out.println("Current Balance: $" + Balance());
if (currentBalance < 0) {
System.out.println(
"You have withdrawn more than you have in current balance.\nYou will be charged a overdraft fee");
}
} else {
System.out.println(
"You have withdrawn more than you have in current balance.\nYou will be charged a overdraft fee");
}
} else {
System.out.println("Can't remove 0 from account");
}
scanner.close();
}
public void Deposit() {
Scanner scanner = new Scanner(System.in);
System.out.println("How much would you like to deposit?");
int depositAmount = scanner.nextInt();
if (depositAmount > 0) {
currentBalance += depositAmount;
Balance();
}
System.out.println("Amount deposited: $" + depositAmount);
System.out.println("Current Balance: $" + Balance());
scanner.close();
}
public int Balance() {
return currentBalance;
}
}
错误信息
答:
0赞
kefomo
10/5/2022
#1
就像有人说的,你应该学习循环。有两种类型:while-loop 和 for-loop。在这种情况下,您应该使用 while 循环。
你的问题可能是这样的。
public class App {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
System.out.println("What woul you like to do?");
String response;
Transactions newTransaction = new Transactions();
while (true) {
response = scanner.next().toLowerCase();
if (response.equals("withdraw")) {
newTransaction.Withdrawal();
break;
} else if (response.equals("deposit")) {
newTransaction.Deposit();
break;
} else {
System.out.println("Please select a valid choice");
}
}
scanner.close();
}
}
评论
0赞
Brian Gomez
10/5/2022
你是对的。我只是错过了中断语句。谢谢。
1赞
Salvatore Allegra
10/5/2022
#2
您可以使用哨兵。哨兵是输入的将结束迭代的值。
//incomplete code showing logic
int choice;
Scanner input = new Scanner(System.in);
System.out.println("Enter Choice");
choice = input.nextInt();
while(choice != -1){ //-1 is the sentinel, can be value you choose
//Some logic you want to do
System.out.println("Enter choice or -1 to end");
choice = input.NextInt(); //notice we input choice again here
}
评论
while
while