我的代码有点困难。在存入30美元的金额后,假设要带回保存菜单 A-存款 B - 退出 C - 报告 D-Return主菜单
不知怎的,它不断扫描30美元,而不是要求新的输入。
感谢任何想要帮助我的人。
package Menu;
import java.util.Scanner;
enum Options {
A, B, C, D
}
public class Menu {
public Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
Menu menu = new Menu();
menu.mainMenu();
}
public void mainMenu() {
String userChoice;
boolean quit = false;
do {
System.out.println("Bank Menu" + "\nA: Savings" + "\nB: Checking"
+ "\nC: Exit");
userChoice = sc.next().toUpperCase();
switch (userChoice) {
case "A":
savingMenu savMen = new savingMenu();
break;
case "B":
break;
case "C":
quit = true;
break;
default:
System.out.println("Wrong entry.");
break;
}
System.out.println();
} while (!quit);
sc.close();
System.exit(0);
}
}
package Menu;
import Savings.Savings;
public class savingMenu extends Menu {
Savings sav = new Savings();
public savingMenu() {
String userChoice;
boolean quit = false;
/**
* A - Deposit B - Withdraw C - Report D - Return Menu
*/
do {
System.out.println("Savings Menu" + "\nA: Deposit "
+ "\nB: Withdraw" + "\nC: Report"
+ "\nD: Return to Bank Menu");
userChoice = sc.nextLine().toUpperCase();
switch (userChoice) {
case "A":
sav.makeDeposit();
break;
case "B":
sav.makeWithdraw();
break;
case "C":
sav.doMonthlyReport();
case "D":
quit = true;
super.mainMenu();
default:
System.out.println("Wrong choice.");
break;
}
System.out.println();
} while (!quit);
}
}
答案 0 :(得分:0)
使用扫描仪检查下一行有hasNextLine()
答案 1 :(得分:0)
好的,我发现了错误,或者我应该说错误理解。
我在帖子中删除的一个班级实际上造成了破坏。
public void validateDepositAmount() {
boolean isDoubleDigit = false;
System.out.println("How much would you like to deposit?");
//try (Scanner sc = new Scanner(System.in)) {
Scanner sc = new Scanner(System.in);
do {
if (sc.hasNextDouble()) {
totalDeposit = sc.nextDouble();
if(totalDeposit >= 0) {
isDoubleDigit = true;
} else {
System.out.println("Your input must higher than 0.");
sc.nextLine();
}
} else {
System.out.println("Please put an amount.");
sc.nextLine();
}
} while (isDoubleDigit == false);
//} catch (Exception e) {
//e.printStackTrace();
//}
}
所以基本上我正在使用Try with resources,它只是继续循环。