gpt4 book ai didi

java - 嵌套的 while 循环不循环?

转载 作者:行者123 更新时间:2023-12-01 14:59:19 25 4
gpt4 key购买 nike

代码如下:

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

}

我认为这些方法对此并不是太重要,因此我将省略它们以节省空间。

该程序的目标是记录和计算多天的交易(金额未知,因此我无法使用 for 循环)。

它顺利地完成了第一次运行,之后,它跳过了内部 while 循环。

我看过大括号,不认为这是问题所在。

最佳答案

您是指Y还是y:

while (keepGoing.equals("Y") || keepGoing.equals("y")) 

您的代码测试了同一件事,即 y,两次。


仅供引用,您的测试可以简化为:

while (keepGoing.equalsIgnoreCase("y")) 

关于java - 嵌套的 while 循环不循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13908914/

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