簡體   English   中英

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

它的 output 是這樣的:

Welcome to ABC BANK 

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

Select an 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!

但是當我輸入它時,它會說這是一個無效輸入。 我應該怎么做才能通過輸入“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”,因此它將 go 放入 else 並打印無效條目。 如果我理解正確的話,在 while 語句之后你實際上不需要:

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

您的代碼有兩個問題:

  1. 將以下代碼放在 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)"); }

    然而,僅僅糾正這個問題是不夠的。 檢查下一個點/問題以了解需要做什么才能使其按您的期望工作。

  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. 永遠不應該關閉System.inScanner ,因為它也會關閉System.in ,並且如果不重新啟動 JVM 就無法再次打開它。但是,請確保關閉文件的Scanner

  2. 你應該養成將塊的主體包含在{...}內的習慣,即使你在主體中只有一個語句。 這將使您免於意外掉出 scope 的某些語句,例如

    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 可以在單擊按鈕或按下組合鍵時格式化代碼。

所以我有這個簡單的銀行程序。 它的主菜單是:

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

它的 output 是這樣的:

Welcome to ABC BANK 

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

Select an 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!

但是當我輸入它時,它會說它是一個無效的輸入。 我應該怎么做才能通過輸入“Q”來結束程序?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM