Java Do-While 循环 - 如何使用 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!");
,

您的代码有两个问题:

  1. 将以下代码置于 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)");
    }
    

    然而,仅仅纠正这个问题是不够的。检查下一点/问题以了解需要做什么才能使其按您的预期工作。

  2. 在无效输入的情况下不循环:您需要另一个循环(我建议如下所示的 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!

一些附加说明:

  1. 您应该永远不要关闭 ScannerSystem.in,因为它也会关闭 System.in 并且无法在不重新启动的情况下再次打开它JVM。但是,请确保关闭文件的 Scanner

  2. 您应该养成将块体包含在 {...} 中的习惯,即使体中只有一条语句。这将使您免于某些意外超出范围的语句,例如

    if (1 == 2)
        System.out.print("Hello");
        System.out.println("World");
    
    System.out.println("What's up?"); 
    

将打印

World
What's up?

而不仅仅是

What's up?
  1. 保持代码格式良好,因为这将帮助您轻松快速地找到问题。您的 IDE 可以通过单击按钮或按下组合键来格式化代码。
,

我更喜欢像 apache-commons-cli

这样的 cli-library