【问题标题】:Do/while loop isn't executing properlyDo/while 循环没有正确执行
【发布时间】:2013-11-21 13:41:58
【问题描述】:

我确定我遗漏了一些简单的东西,但我无法让我的 do while 循环正确执行。我希望它第一次运行并继续运行,直到用户输入 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();

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

    }
}

【问题讨论】:

    标签: java loops while-loop


    【解决方案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
    }
    

    【讨论】:

    • 我返回并添加了缺少的大括号,但现在它循环并询问要访问的帐户,并立即询问存款金额,而无需等待任何用户输入。这是程序的输出:您想访问哪个帐户(常规帐户或储蓄帐户)?您希望执行什么操作(存款、取款或每月流程)?存款 你想存多少钱? 44 您的余额为 44.0 您想访问哪个账户(普通账户或储蓄账户)?你想存多少钱?
    • 我用我的例子低估了 - 你的花括号应该包含你希望在特定上下文中执行的所有操作(即“常规”)。
    【解决方案2】:

    您必须确保一开始就阅读了这两个选项; 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"));
       }
    }
    

    【讨论】: