作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我确信我错过了一些简单的东西,但我无法让我的 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++)的默认行为是仅执行 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
}
关于java - Do/while 循环未正确执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20112959/
我是一名优秀的程序员,十分优秀!