【问题标题】:Java Do-While Loop - How to End the Program Using Q OptionJava Do-While 循环 - 如何使用 Q 选项结束程序
【发布时间】:2021-04-25 17:19:44
【问题描述】:

所以我有这个简单的银行程序。它的主菜单是:

  • 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'来结束程序?

【问题讨论】:

  • (anotherTransact == 'N' || anotherTransact == 'n')anotherTransact == 'Q'扩展它

标签: java loops if-statement do-while


【解决方案1】:

你的 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!");

【讨论】:

    【解决方案2】:

    你的代码有两个问题:

    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 可以在单击按钮或按下组合键时格式化代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-24
      • 2018-04-06
      • 1970-01-01
      • 1970-01-01
      • 2020-08-26
      • 2020-11-26
      • 1970-01-01
      相关资源
      最近更新 更多