我正在研究一个简单的ATM程序,我试图在switch语句中使用if / else语句来保持或退出循环。
到目前为止,这是我的代码:
while(continues == true)
{
System.out.println("Please select one of the following:\r\n"
+ "1 - Deposit\r\n"
+ "2 - Withdrawal\r\n"
+ "3 - Check Balance\r\n"
+ "4 - Log Out");
accountChoice = menuChoice.nextByte();
switch(accountChoice)
{
case 1:
System.out.println("How much would you like to Deposit?");
depositAmt = deposit.nextDouble();
bob.deposit(depositAmt);
System.out.println("Thank you, your current balance "
+ " is $" + bob.getBalance());
System.out.println("Do you want to make another "
+ "transaction?");
yesNo = yN.next();
if(yesNo == "Y" || yesNo == "y")
{
System.out.println("You will make another transaction");
continues = true;
break;
}
else
{
System.out.println("You will not make another transaction");
continues = false;
break;
}
case 2:
System.out.println("You chose Withdrawal");
break;
case 3:
System.out.println("You chose Check Balance");
break;
case 4:
System.out.println("You chose Log Out");
continues = false;
break;
}// end switch
}// end while loop
我的输出是“你不会进行另一次交易”,然后我的程序结束,无论我实际输入什么。我的问题似乎是总是挑选else语句,但我不知道为什么。
非常感谢任何帮助。