Java Do-While 循环 - 如何使用 Q 选项结束程序
Java Do-While Loop - How to End the Program Using Q Option
所以我有这个简单的银行程序。它的主菜单是:
- B - 检查余额
- D - 存款
- W - 取款
- Q - 退出
他们的功能基本上就是他们的名字。有两种方法可以终止程序:输入 '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!");
您的代码有两个问题:
将以下代码置于 do-while
循环范围之外:
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)");
}
但是,仅仅纠正这个问题是不够的。检查下一个 point/problem 以了解需要做什么才能使其按您的期望工作。
输入无效不回环:你需要另一个循环(我推荐do-while
循环如下图)来纠正这个问题.
boolean valid;
do {
valid = true;
System.out.print("\nWant to Transact another (Y/N?) ");
anotherTransact = scan.next().charAt(0);
System.out.println("\n======================================================");
if (anotherTransact == 'N' || anotherTransact == 'n') {
System.out.println("\nThank you for using this program!");
} else if (!(anotherTransact == 'Y' || anotherTransact == 'y')) {
System.out.println("Invalid entry, entery any valid option : (Y/N)");
valid = false;
}
} while (!valid);
完整更新代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
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)");
}
boolean valid;
do {
valid = true;
System.out.print("\nWant to Transact another (Y/N?) ");
anotherTransact = scan.next().charAt(0);
System.out.println("\n======================================================");
if (anotherTransact == 'N' || anotherTransact == 'n') {
System.out.println("\nThank you for using this program!");
} else if (!(anotherTransact == 'Y' || anotherTransact == 'y')) {
System.out.println("Invalid entry, entery any valid option : (Y/N)");
valid = false;
}
} while (!valid);
} while (anotherTransact == 'Y' || anotherTransact == 'y');
}
}
样本运行:
Welcome to ABC BANK
B - Check for Balance
D - Make Deposit
W - Make Withdraw
Q - Quit
Select an option : b
Your current balance is 100000
Want to Transact another (Y/N?) d
======================================================
Invalid entry, entery any valid option : (Y/N)
Want to Transact another (Y/N?) d
======================================================
Invalid entry, entery any valid option : (Y/N)
Want to Transact another (Y/N?) y
======================================================
Welcome to ABC BANK
B - Check for Balance
D - Make Deposit
W - Make Withdraw
Q - Quit
Select an option : d
Enter amount to Deposit : 200
Deposit Transaction is successfully completed.
Want to Transact another (Y/N?) y
======================================================
Welcome to ABC BANK
B - Check for Balance
D - Make Deposit
W - Make Withdraw
Q - Quit
Select an option : b
Your current balance is 100200
Want to Transact another (Y/N?) n
======================================================
Thank you for using this program!
一些附加说明:
你应该永远不要为System.in
关闭 Scanner
因为它也会关闭System.in
并且没有无需重新启动 JVM 即可再次打开它的方法。但是,请确保关闭文件 Scanner
。
您应该养成将块主体包含在 {...}
中的习惯,即使主体中只有一条语句。这将使您免于意外超出范围的某些语句,例如
if (1 == 2)
System.out.print("Hello");
System.out.println("World");
System.out.println("What's up?");
将打印
World
What's up?
不只是
What's up?
- 保持您的代码格式正确,因为这将帮助您轻松快速地找到问题。您的 IDE 可以在单击按钮或按组合键时格式化代码。
所以我有这个简单的银行程序。它的主菜单是:
- B - 检查余额
- D - 存款
- W - 取款
- Q - 退出
他们的功能基本上就是他们的名字。有两种方法可以终止程序:输入 '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!");
您的代码有两个问题:
将以下代码置于
do-while
循环范围之外: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)"); }
但是,仅仅纠正这个问题是不够的。检查下一个 point/problem 以了解需要做什么才能使其按您的期望工作。
输入无效不回环:你需要另一个循环(我推荐
do-while
循环如下图)来纠正这个问题.boolean valid; do { valid = true; System.out.print("\nWant to Transact another (Y/N?) "); anotherTransact = scan.next().charAt(0); System.out.println("\n======================================================"); if (anotherTransact == 'N' || anotherTransact == 'n') { System.out.println("\nThank you for using this program!"); } else if (!(anotherTransact == 'Y' || anotherTransact == 'y')) { System.out.println("Invalid entry, entery any valid option : (Y/N)"); valid = false; } } while (!valid);
完整更新代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
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)");
}
boolean valid;
do {
valid = true;
System.out.print("\nWant to Transact another (Y/N?) ");
anotherTransact = scan.next().charAt(0);
System.out.println("\n======================================================");
if (anotherTransact == 'N' || anotherTransact == 'n') {
System.out.println("\nThank you for using this program!");
} else if (!(anotherTransact == 'Y' || anotherTransact == 'y')) {
System.out.println("Invalid entry, entery any valid option : (Y/N)");
valid = false;
}
} while (!valid);
} while (anotherTransact == 'Y' || anotherTransact == 'y');
}
}
样本运行:
Welcome to ABC BANK
B - Check for Balance
D - Make Deposit
W - Make Withdraw
Q - Quit
Select an option : b
Your current balance is 100000
Want to Transact another (Y/N?) d
======================================================
Invalid entry, entery any valid option : (Y/N)
Want to Transact another (Y/N?) d
======================================================
Invalid entry, entery any valid option : (Y/N)
Want to Transact another (Y/N?) y
======================================================
Welcome to ABC BANK
B - Check for Balance
D - Make Deposit
W - Make Withdraw
Q - Quit
Select an option : d
Enter amount to Deposit : 200
Deposit Transaction is successfully completed.
Want to Transact another (Y/N?) y
======================================================
Welcome to ABC BANK
B - Check for Balance
D - Make Deposit
W - Make Withdraw
Q - Quit
Select an option : b
Your current balance is 100200
Want to Transact another (Y/N?) n
======================================================
Thank you for using this program!
一些附加说明:
你应该永远不要为
System.in
关闭Scanner
因为它也会关闭System.in
并且没有无需重新启动 JVM 即可再次打开它的方法。但是,请确保关闭文件Scanner
。您应该养成将块主体包含在
{...}
中的习惯,即使主体中只有一条语句。这将使您免于意外超出范围的某些语句,例如if (1 == 2) System.out.print("Hello"); System.out.println("World"); System.out.println("What's up?");
将打印
World
What's up?
不只是
What's up?
- 保持您的代码格式正确,因为这将帮助您轻松快速地找到问题。您的 IDE 可以在单击按钮或按组合键时格式化代码。