我想在Java的while循環中使用switch語句,但出現了問題。請看看在下面的代碼示例這也解釋了我的問題:在while循環中使用switch語句
Scanner input=new Scanner(System.in);
int selection = input.nextInt();
while (selection<4)
{ switch(selection){
case 1:
System.out.println("Please enter amount");
double amount=input.nextDouble(); //object of scanner class
break;
case 2:
System.out.println("Enter ID number");
break;
case 3:
System.out.println("Enter amount to be credited");
break;
}
System.out.println("1. Transfer\n2.Check balance\n3.Recharge");
}
如果我運行該代碼時,輸出如下:
1
Please enter amount
2000
1. Transfer
2.Check balance
3.Recharge
Please enter amount
2
1. Transfer
2.Check balance
3.Recharge
Please enter amount
當我輸入金額,然後,我會喜歡選擇另一個選項 - 輸出應該根據選擇的選項(你應該知道我想要這個代碼做什麼)。有人可以幫助糾正代碼嗎?
感謝
您提示值,則不會再提示。你只需循環使用你在開始時提示的SAME值。 –