2013-11-21 251 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