2013-11-21 145 views
0

我確定我缺少一些簡單的東西,但我無法讓我的while while循環正確執行。我希望它能夠第一次運行並持續到用戶輸入q。它目前執行一次,然後循環備份以詢問要訪問的帳戶,然後什麼都不做。任何幫助或指引我在正確的方向,所以我可以修復它將不勝感激。Do/while循環執行不正確

public class Crawford_Driver 
{ 

    public static void main(String[] args) 
    { 
     Scanner keyboard = new Scanner(System.in); 
     double input1; 
     String accountChoice; 
     String accountActivity; 
     RegularAccount regAcct = new RegularAccount(0, .5); 
     SavingsAccount savAcct = new SavingsAccount(0, .5); 


     do{ 
      System.out.println("What account would you like to access(regular or savings)?"); 
      accountChoice = keyboard.nextLine(); 

      if(accountChoice.equalsIgnoreCase("regular")) 

       System.out.println("What action do you wish to perform(deposit, withdraw or monthly process)? "); 
      accountActivity = keyboard.nextLine(); 

      if (accountActivity.equalsIgnoreCase("deposit")) 
       { 
        System.out.println("How much would you like to deposit?"); 
        input1= keyboard.nextDouble(); 
        regAcct.deposit(input1); 
        System.out.println("Your balance is " + regAcct.getBalance()); 
       } 
      else if (accountActivity.equalsIgnoreCase("withdraw")) 
       { 
        System.out.println("How much would you like to withdraw?"); 
        input1= keyboard.nextDouble(); 
        regAcct.withdraw(input1); 
        System.out.println("Your balance is "+ regAcct.getBalance()); 
       } 
      else if (accountActivity.equalsIgnoreCase("monthly process")) 
       { 
        regAcct.monthlyProcess(); 
       } 
      else { 
       if (accountChoice.equalsIgnoreCase("savings")) 

        if (accountActivity.equalsIgnoreCase("deposit")) 
         { 
          System.out.println("How much would you like to deposit?"); 
          input1= keyboard.nextDouble(); 
          savAcct.deposit(input1); 
          System.out.println("Your balance is " + savAcct.getBalance()); 
         } 

       if (accountActivity.equalsIgnoreCase("withdraw")) 

        System.out.println("How much would you like to withdraw?"); 
       input1= keyboard.nextDouble(); 
       savAcct.withdraw(input1); 
       System.out.println("Your balance is "+ savAcct.getBalance()); 

      } 
     }while (!accountChoice.equalsIgnoreCase("Q")); 

    } 
} 

回答

0

你必須確保你讀一開始這兩個選項; accountChoiceaccountActivity。您的代碼有一個問題是缺少{}的用法。

所以它會像...

do { // do below first before checking the while condition 
    if(accountChoice.equalsIgnoreCase("regular")) 
    { 
     // do your work. 
    } else if (accountChoice.equalsIgnoreCase("savings")) 
    { 
     // do the rest 
    } 
} while (!accountChoice.equalsIgnoreCase("Q")); 

下面是修改後的代碼。

public class Crawford_Driver 
{ 

    public static void main(String[] args) 
    { 
     Scanner keyboard = new Scanner(System.in); 
     double input1; 
     String accountChoice; 
     String accountActivity; 
     RegularAccount regAcct = new RegularAccount(0, .5); 
     SavingsAccount savAcct = new SavingsAccount(0, .5); 


     do { 
      System.out.println("What account would you like to access(regular or savings)?"); 
      accountChoice = keyboard.nextLine(); 

      System.out.println("What action do you wish to perform(deposit, withdraw or monthly process)? "); 
      accountActivity = keyboard.nextLine(); 


      if(accountChoice.equalsIgnoreCase("regular")) { // LINE moved and BRACKET MISSING 

       if (accountActivity.equalsIgnoreCase("deposit")) 
       { 
        System.out.println("How much would you like to deposit?"); 
        input1= keyboard.nextDouble(); 
        regAcct.deposit(input1); 
        System.out.println("Your balance is " + regAcct.getBalance()); 
       } 
       else if (accountActivity.equalsIgnoreCase("withdraw")) 
       { 
        System.out.println("How much would you like to withdraw?"); 
        input1= keyboard.nextDouble(); 
        regAcct.withdraw(input1); 
        System.out.println("Your balance is "+ regAcct.getBalance()); 
       } 
       else if (accountActivity.equalsIgnoreCase("monthly process")) 
       { 
        regAcct.monthlyProcess(); 
       } 
      } 
      else if (accountChoice.equalsIgnoreCase("savings")) 
      { // line changed & BRACKET MISSING 

       if (accountActivity.equalsIgnoreCase("deposit")) 
       { 
        System.out.println("How much would you like to deposit?"); 
        input1= keyboard.nextDouble(); 
        savAcct.deposit(input1); 
        System.out.println("Your balance is " + savAcct.getBalance()); 
       } 
       else if (accountActivity.equalsIgnoreCase("withdraw")) 
       { // bracket missing 

        System.out.println("How much would you like to withdraw?"); 
        input1= keyboard.nextDouble(); 
        savAcct.withdraw(input1); 
        System.out.println("Your balance is "+ savAcct.getBalance()); 
       } 
      } 
     } while (!accountChoice.equalsIgnoreCase("Q")); 
    } 
} 
1

你缺少一個大括號對這一說法後:

if(accountChoice.equalsIgnoreCase("regular")) 

...及本聲明:

if (accountActivity.equalsIgnoreCase("withdraw")) 

爲Java(和C的默認行爲,和C++)僅在執行ifforwhile語句後執行下一個行,如果大括號是省略。

當你完成後,你的說法應該是這樣的:

if(accountChoice.equalsIgnoreCase("regular")) { 
    System.out.println("What action do you wish to perform(deposit, withdraw or monthly process)? "); 
    accountActivity = keyboard.nextLine(); 
    // Rest of code that concerns activity with a "regular" account 
} 
+0

我回去,並添加缺少的括號但是現在它遍歷並詢問什麼帳戶訪問,並立即問多少,而無需等待任何用戶輸入存入。 以下是該計劃的輸出: 您想訪問哪個帳戶(常規或儲蓄)? 定期 您希望執行什麼操作(存款,提款或每月處理)? 存款 您要存多少錢? 您的餘額是44.0 您想要訪問哪個帳戶(常規或儲蓄)? 你想存多少錢? –

+0

我突出了我的例子 - 你的花括號應該包裝你想要在特定環境下做的所有事情(即「常規」)。 – Makoto