简体   繁体   English

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

[英]Java Do-While Loop - How to End the Program Using Q Option

so i have this simple bank program.所以我有这个简单的银行程序。 it has main menus which are:它的主菜单是:

  • B - Check for Balance B - 检查余额
  • D - Make Deposit D - 存款
  • W - Make Withdraw W - 取款
  • Q - Quit Q - 退出

their functions are basically their names.他们的功能基本上就是他们的名字。 and there are two ways to terminate the program: by inputting 'Q' and 'N'.but I have a problem with the Q - Quit option.有两种方法可以终止程序:输入“Q”和“N”。但是我对 Q - Quit 选项有疑问。 here's the source code:这是源代码:

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();

and its output would be like this:它的 output 是这样的:

Welcome to ABC BANK 

B - Check for Balance
D - Make Deposit
W - Make Withdraw
Q - Quit

Select an option :

The output should be like this when i input the Q option:当我输入 Q 选项时,output 应该是这样的:

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!

but when i input it, it would say that it is an invalid input.但是当我输入它时,它会说这是一个无效输入。 what should i do to end the program by inputting 'Q'?我应该怎么做才能通过输入“Q”来结束程序?

Your Q implementation is correct, it will exit the do while loop, but after the while statement you have an if:您的 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)");
        }

When you type Q to exit the loop anotherTransact is not equals to 'N' therefore it will go into the else and print invalid entry.当您键入 Q 退出循环时, anotherTransact不等于“N”,因此它将 go 放入 else 并打印无效条目。 If I understand correctly you dont actually need after the while statement is:如果我理解正确的话,在 while 语句之后你实际上不需要:

 System.out.println("\n======================================================"); 
 System.out.println("\nThank you for using this program!");

There are two problems with your code:您的代码有两个问题:

  1. Putting the following code outside the scope of do-while loop:将以下代码放在 scope 的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)"); }

    However, just correcting this problem is not sufficient.然而,仅仅纠正这个问题是不够的。 Check the next point/problem to understand what needs to be done in order to get it working as per your expectation.检查下一个点/问题以了解需要做什么才能使其按您的期望工作。

  2. Not looping back in case of invalid input: You need another loop (I recommend do-while loop as shown below) to rectify this 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);

The full updated code:完整的更新代码:

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');
    }
}

A sample run:示例运行:

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!

Some additional side notes:一些附加说明:

  1. You should never close the Scanner for System.in as it will also close System.in and there is no way to open it again without restarting the JVM. However, make sure to close a Scanner for a file.永远不应该关闭System.inScanner ,因为它也会关闭System.in ,并且如果不重新启动 JVM 就无法再次打开它。但是,请确保关闭文件的Scanner

  2. You should make a habit of enclosing the body of a block within {...} even if you have a single statement in the body.你应该养成将块的主体包含在{...}内的习惯,即使你在主体中只有一个语句。 This will save you from some statement accidentally falling out of scope eg这将使您免于意外掉出 scope 的某些语句,例如

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

will print将打印

World
What's up?

and not just而不仅仅是

What's up?
  1. Keep your code well-formatted because it will help you find the problem easily and quickly.保持代码格式正确,因为它可以帮助您轻松快速地找到问题。 Your IDE can format the code on click of a button or when you press a combination of keys.您的 IDE 可以在单击按钮或按下组合键时格式化代码。

so i have this simple bank program.所以我有这个简单的银行程序。 it has main menus which are:它的主菜单是:

  • B - Check for Balance B - 检查余额
  • D - Make Deposit D - 存款
  • W - Make Withdraw W - 取款
  • Q - Quit Q - 退出

their functions are basically their names.它们的功能基本上就是它们的名字。 and there are two ways to terminate the program: by inputting 'Q' and 'N'.but I have a problem with the Q - Quit option.并且有两种方法可以终止程序:通过输入“Q”和“N”。但我对 Q - Quit 选项有疑问。 here's the source code:这是源代码:

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();

and its output would be like this:它的 output 是这样的:

Welcome to ABC BANK 

B - Check for Balance
D - Make Deposit
W - Make Withdraw
Q - Quit

Select an option :

The output should be like this when i input the Q option:当我输入 Q 选项时,output 应该是这样的:

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!

but when i input it, it would say that it is an invalid input.但是当我输入它时,它会说它是一个无效的输入。 what should i do to end the program by inputting 'Q'?我应该怎么做才能通过输入“Q”来结束程序?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM