所以我有这个简单的银行程序。它的主菜单是:
它们的功能基本上就是它们的名字。有两种方法可以终止程序:通过输入“Q”和“N”。但是我对 Q - Quit 选项有疑问。这是源代码:
Scanner scan = new Scanner (System.in);
char anotherTransact = 0, option;
int balance = 100000, deposit = 0, withdraw = 0;
do {
System.out.println("\nWelcome to ABC BANK \n");
System.out.println("B - Check for Balance");
System.out.println("D - Make Deposit");
System.out.println("W - Make Withdraw");
System.out.println("Q - Quit");
System.out.print("\nSelect an option : ");
option = scan.next().charAt(0);
if ((option == 'B') || (option == 'b')) {
System.out.println("\nYour current balance is " +balance);
}
else if ((option == 'D') || (option == 'd')) {
System.out.print("\nEnter amount to Deposit : ");
deposit = scan.nextInt();
if (deposit > 1 && deposit <= 500000) {
System.out.println("Deposit Transaction is successfully completed.");
balance = balance + deposit;
}
else if (deposit > 500000) {
System.out.println("Deposit Amount must not be greater than 500,000");
}
else {
System.out.println("Deposit must be greater than zero");
}
}
else if ((option == 'W') || (option == 'w')) {
System.out.print("\nEnter amount to Withdraw : ");
withdraw = scan.nextInt();
if ((withdraw % 100 == 0 && withdraw < 150000)) {
System.out.println("Withdrawal Transaction is successfully completed.");
balance = balance - withdraw;
}
else if (withdraw > 150000) {
System.out.println("Withdrawal Amount must not be greater then 150,000");
}
else if (withdraw < 0) {
System.out.println("Withdrawal Amount must be greater than zero");
}
else {
System.out.println("Withdawal Amount must be divisible by 100");
}
}
else if ((option == 'Q') || (option == 'q'))
break;
else {
System.out.println("\nInvalid entry, enter any valid option : (B/D/W/Q)");
}
System.out.print("\nWant to Transact another (Y/N?) ");
anotherTransact = scan.next().charAt(0);
System.out.println("\n======================================================");
}
while ((anotherTransact == 'Y') || (anotherTransact =='y'));
if ((anotherTransact == 'N' || anotherTransact == 'n')) {
System.out.println("\nThank you for using this program!");
}
else {
System.out.println("Invalid entry, entery any valid option : (Y/N)");
}
scan.close();
它的输出是这样的:Welcome to ABC BANK
B - Check for Balance
D - Make Deposit
W - Make Withdraw
Q - Quit
Select an option :
当我输入 Q 选项时,输出应该是这样的:Welcome to ABC BANK
B - Check for Balance
D - Make Deposit
W - Make Withdraw
Q - Quit
Select an option : Q
======================================================
Thank you for using this program!
但是当我输入它时,它会说这是一个无效的输入。我应该怎么做才能通过输入“Q”来结束程序?
最佳答案
您的 Q 实现是正确的,它将退出 do while 循环,但在 while 语句之后您有一个 if:
if ((anotherTransact == 'N' || anotherTransact == 'n')) {
System.out.println("\nThank you for using this program!");
}
else {
System.out.println("Invalid entry, entery any valid option : (Y/N)");
}
当您键入 Q 退出循环时 anotherTransact
不等于 'N' 因此它将进入 else 并打印无效条目。如果我理解正确的话,在 while 语句之后你实际上并不需要: System.out.println("\n======================================================");
System.out.println("\nThank you for using this program!");
关于Java Do-While 循环 - 如何使用 Q 选项结束程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65824858/