gpt4 book ai didi

java - Do/while 循环未正确执行

转载 作者:行者123 更新时间:2023-12-01 13:49:03 24 4
gpt4 key购买 nike

我确信我错过了一些简单的东西,但我无法让我的 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"))

Java(以及 C 和 C++)的默认行为是仅执行 iffor 或之后的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
}

关于java - Do/while 循环未正确执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20112959/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com