[英]Nested while loop not looping?
Here's the code: 这是代码:
while (keepGoingDay.equals("y") || keepGoingDay.equals("y")){
System.out.println(acct1);
System.out.println(acct2);
Account.reset();
while (keepGoing.equals("y") || keepGoing.equals("y"))
{
//get account number, what to do, and amount
System.out.print("\nEnter the number of the account you would like to access: ");
acctNumber = scan.nextLong();
System.out.print("Would you like to make a deposit (D) or withdrawal (W)? ");
action = scan.next();
System.out.print("Enter the amount: ");
amount = scan.nextDouble();
if (amount > 0)
if (acctNumber == acct1.getAcctNumber())
if (action.equals("w") || action.equals("W"))
acct1.withdraw(amount);
else if (action.equals("d") || action.equals("D"))
acct1.deposit(amount);
else
System.out.println("Sorry, invalid action.");
else if (acctNumber == acct2.getAcctNumber())
if (action.equals("w") || action.equals("W"))
acct1.withdraw(amount);
else if (action.equals("d") || action.equals("D"))
acct1.deposit(amount);
else
System.out.println("Sorry, invalid action.");
else
System.out.println("Sorry, invalid account number.");
else
System.out.println("Sorry, amount must be > 0.");
System.out.print("\nMore transactions? (y/n)");
keepGoing = scan.next();
}
System.out.println("End of day stats: ");
System.out.println("Number of deposits: " + Account.getNumDeposits());
System.out.println("Number of withdrawals: " + Account.getNumWithdrawals());
System.out.println("Total value of deposits: " + Account.getTotalDeposits());
System.out.println("Total value of withdrawals: " + Account.getTotalWithdrawals());
System.out.print("More days?");
keepGoingDay = scan.next();
}
}
I don't think the methods are too essential to this so I'll leave them out to save space. 我认为这些方法并不需要太重要,因此我省去了它们以节省空间。
The goal of this program is to have transactions be recorded and counted for multiple days (unknown amount so I couldn't use a for loop). 该程序的目的是要记录并记录数天的交易(金额未知),所以我无法使用for循环。
It goes through the first run fine and after that, it skips over the inner while loop. 它经过第一次运行,然后跳过内部while循环。
I've looked at the braces and don't think that's the issue. 我看过牙套,但不认为这是问题所在。
Did you mean Y
or y
: 您是说
Y
还是y
:
while (keepGoing.equals("Y") || keepGoing.equals("y"))
You code was testing the same thing, ie y
, twice. 您的代码对同一件事(即
y
)进行了两次测试。
FYI, your tests could be simplified to: 仅供参考,您的测试可以简化为:
while (keepGoing.equalsIgnoreCase("y"))
put scan.next();
把
scan.next();
after your acctNumber = scan.nextLong();
在您的
acctNumber = scan.nextLong();
line and amount = scan.nextDouble();
行和
amount = scan.nextDouble();
line. 线。
Like this. 像这样。 check my newly added lines.
检查我新添加的行。
System.out.print("\nEnter the number of the account you would like to access: ");
acctNumber = scan.nextLong();
scan.next(); // newly added
System.out.print("Would you like to make a deposit (D) or withdrawal (W)? ");
action = scan.next();
System.out.print("Enter the amount: ");
amount = scan.nextDouble();
scan.next(); // newly added
If I had the flexibility to change the skeleton program, I would have used boolean variables. 如果我可以灵活地更改框架程序,则可以使用布尔变量。
However, the solution that you have to put in place is to reset keepGoing to "y" because it was set to "n" causing it to be skipped. 但是,您必须采用的解决方案是将keepGoing重置为“ y”,因为将其设置为“ n”会导致其被跳过。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.