Java Do-While 循环 - 如何使用 Q 选项结束程序

标签 java loops if-statement do-while

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

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

    关于Java Do-While 循环 - 如何使用 Q 选项结束程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65824858/

    相关文章:

    java - 通过notepad++默认java代码打开文件txt

    java - <form :errors> tag in my Spring 3. 0.3 web MVC 应用程序未呈现验证错误

    Python 使用列表理解读取行(csv 和 json 文件)

    asp.net-mvc - 循环遍历作为列表传递的模型中的列表

    java - JAVA 读取文本文件时出现多个分隔符错误

    java - 使java Swing 框架可移动并设置未装饰

    iphone - 如何在 iOS 5 中运行多个循环和条件检查?

    c++ - If语句问题

    c++ - 具有多个条件的 If 语句

    javascript - 如何生成 if 语句的所有可能结果?