2016-01-03 28 views
2

我有2個班。這個程序非常模擬ATM。用戶將要求存入,提取或查看用戶銀行賬戶的餘額。如何在不退出程序的情況下在do-while循環中添加if語句?

首先餘額將設置爲零,然後用戶可以存款或取款。

第一類稱爲CheckingAccount將有方法和沒有主要方法,而TestCheckingAccount有主要方法。所有輸入和輸出將在TestCheckingAccount類中進行,而CheckingAccount進行計算。

如果用戶選擇存款,則賬戶的餘額將被添加到用戶想要存入他/她的賬戶的金額。 CheckingAccount類中的getBalance方法返回用戶帳戶餘額,而processDeposit將用戶想要存入的金額添加到餘額,而processCheck則減少用戶選擇餘額的金額。

如果用戶選擇查看餘額,那麼它將打印用戶餘額。

的CheckingAccount類

public class CheckingAccount{ 
    double myBalance = 0; 

    public double getBalance(){ 
     return myBalance; 
    } 

    public void processDeposit(double amount){ 
     myBalance += amount; 
    } 

    public void processCheck(double amount){ 
     myBalance -= amount; 
    } 
} 

TestCheckingAccount類

import java.util.Scanner; 
public class TestCheckingAccount { 

public static void main(String[] args) { 

    CheckingAccount check = new CheckingAccount(); 

    Scanner in = new Scanner(System.in); 

    String choice; 
    double moneyAdded; 
    double moneySubtracted; 

    do 
    { 
     System.out.println("Deposit"); 
     System.out.println("Withdraw"); 
     System.out.println("Balance"); 
     System.out.println("Exit"); 
     System.out.println("**********************************************"); 
     System.out.println("Which operation would you like to use?"); 
     choice = in.nextLine(); 

     if(choice.equalsIgnoreCase("Deposit")) 
     { 
      System.out.println("How much money would you like to deposit?"); 
      moneyAdded = in.nextDouble(); 
      check.processDeposit(moneyAdded); 
     } 

     if(choice.equalsIgnoreCase("Withdraw")) 
     { 
      System.out.println("How much money would you like to withdraw?"); 
      moneySubtracted = in.nextDouble(); 

      check.processCheck(moneySubtracted); 
     } 
     else if(choice.equalsIgnoreCase("Balance")) 
     { 
      System.out.printf("$%.2f%n" , check.myBalance); 
     } 
     else if(choice.equalsIgnoreCase("Exit")) 
     { 
      System.exit(0); 
     } 

    } 
    while(choice.equalsIgnoreCase("Deposit")||choice.equalsIgnoreCase("Withdraw")||choice.equalsIgnoreCase("Balance")); 

    in.close(); 

} 

}

程序運行正常,但是,我遇到的問題是,程序必須詢問用戶是否他/她想存入,取出或查看餘額,直到用戶完成並退出程序。

因此,當程序詢問用戶是否要在賬戶中存入,提取或查看餘額時,該部分必須處於循環狀態。我把它放在一個do-while循環中。因此,當我運行程序並要求餘額打印出餘額,然後循環良好,然後程序再次詢問他/她是否想要提取,存款,查看餘額或退出。

但是,當我要求程序退出或存款然後它運行但不循環,而是退出循環。

我怎樣才能使程序,使其可以提取或存款,但仍然循環,並返回到菜單,詢問用戶他/她是否要存款或取款,而不是結束循環?

回答

5

您可以在「退出」條件下使用無限循環並使用break。此外,您可以使用try-with-resources close statement關閉您的Scanner。喜歡的東西,

try (Scanner in = new Scanner(System.in)) {  
    String choice; 
    double moneyAdded; 
    double moneySubtracted; 

    while (true) { 
    System.out.println("Deposit"); 
    System.out.println("Withdraw"); 
    System.out.println("Balance"); 
    System.out.println("Exit"); 
    System.out.println("**********************************************"); 
    System.out.println("Which operation would you like to use?"); 
    choice = in.nextLine(); 

    if (choice.equalsIgnoreCase("Deposit")) { 
     System.out.println("How much money would you like to deposit?"); 
     moneyAdded = in.nextDouble(); 
     check.processDeposit(moneyAdded); 
    } else if (choice.equalsIgnoreCase("Withdraw")) { 
     System.out.println("How much money would you like to withdraw?"); 
     moneySubtracted = in.nextDouble(); 
     check.processCheck(moneySubtracted); 
    } else if (choice.equalsIgnoreCase("Balance")) { 
     System.out.printf("$%.2f%n", check.myBalance); 
    } else if (choice.equalsIgnoreCase("Exit")) { 
     break; 
    } 
    } 
} 
2

正在發生的事情,我認爲是它讀一空行已經閱讀了雙之後,並沒有正在檢查在while條件。爲了幫助您學習,您應該設置一個斷點或一些調試並檢查它。

但是它會更容易只指定當你想使用(!),而不是當你想留在這,這意味着你不需要的選項字符串的repeat the comparisons退出循環:

do 
{ 
    ... //this will repeat until they type Exit 
} 
while (!choice.equalsIgnoreCase("Exit")); 

你會隨後也刪除代碼:

//else if(choice.equalsIgnoreCase("Exit")) 
//{ 
// System.exit(0); 
//} 
3

使用掃描器的下一個方法,而不是nextLine。

choice = in.next(); 

nextDouble不考慮最後一個換行符,因此它改爲nextLine方法。這意味着nextLine會在下一次讀取換行符時要求用戶輸入(空輸入)。由於輸入不符合do循環繼續的要求,程序將退出。

我希望答案可以幫助你。

相關問題