我确定我错过了一些简单的东西,但是我无法让我的do循环执行得当。我希望它在第一次运行并继续运行直到用户输入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"));
}
}
答案 0 :(得分:1)
在这句话后你错过了一组花括号:
if(accountChoice.equalsIgnoreCase("regular"))
......和这句话:
if (accountActivity.equalsIgnoreCase("withdraw"))
Java(以及C和C ++)的默认行为是在if
,for
或while
语句后仅执行 next 行如果省略花括号。
完成后,您的陈述应如下所示:
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
}
答案 1 :(得分:0)
您必须确保在开始时阅读这两个选项; accountChoice
和accountActivity
。您的代码存在的一个问题是缺少{
和}
用法。
所以它就像......
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"));
}
}