Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Do/while循环是';执行不当_Java_Loops_While Loop - Fatal编程技术网

Java Do/while循环是';执行不当

Java Do/while循环是';执行不当,java,loops,while-loop,Java,Loops,While Loop,我肯定我错过了一些简单的东西,但我不能让我的do-while循环正确执行。我希望它第一次运行并一直运行,直到用户输入q。它当前执行一次,然后循环返回,询问要访问哪个帐户,然后什么也不做。如果您能为我提供帮助或为我指明正确的方向,我将不胜感激 public class Crawford_Driver { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in

我肯定我错过了一些简单的东西,但我不能让我的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"));

    }
}

此语句后缺少一组大括号:

if(accountChoice.equalsIgnoreCase("regular"))
 if (accountActivity.equalsIgnoreCase("withdraw"))
……本声明:

if(accountChoice.equalsIgnoreCase("regular"))
 if (accountActivity.equalsIgnoreCase("withdraw"))
Java(以及C和C++)的默认行为是只执行
if
for
,或者
while
语句后面的下一行,如果省略了大括号

完成后,您的声明应如下所示:

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
}

您必须确保在开始时阅读这两个选项<代码>账户选择和
账户活动
。代码的一个问题是缺少
{
}
用法

所以这就像

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

我返回并添加了缺少的大括号,但现在它循环通过,询问访问哪个帐户,并立即询问存款金额,而无需等待任何用户输入。以下是程序的输出:您希望访问哪个帐户(普通帐户还是储蓄帐户)?定期您希望执行什么操作(存款、取款或每月流程)?你想存多少钱?44您的余额为44.0您想使用哪个帐户(普通帐户还是储蓄帐户)?你想存多少钱?我理解了我的例子——你的花括号应该在特定的上下文中(即“常规”)包装你想做的所有事情。