嗨即時嘗試輸出一行,當用戶輸入一定的數字。然而,即使你輸入另一個數字,同一行仍然出現。我究竟做錯了什麼?使用if else語句
public class apples{
public static void main(String args[]){
int input1=1;
int input2=2;
int input3=3;
int input4=4;
System.out.println("Welcome to MK Supermarket");
System.out.println("Four items are available for sale:");
System.out.println("1.Flour 2.Crix 3.Juice 4.Cereal");
Scanner sales = new Scanner (System.in);
System.out.println("Press 1 for Flour, Press 2 for Crix, Press 3 for Juice, Press 4 for Cereal");
int itemSelect=sales.nextInt();
if(input1==1){
System.out.println("You chose Flour");
int price1=30;
}
else if(input2==2){
System.out.println("You chose Crix");
int price2=10;
}
else if(input3==3){
System.out.println("You chose Juice");
int price3=15;
}
else if(input4==4){
System.out.println("You chose Cereal");
int price4=25;
}
else{
System.out.println("Invalid Entry");
}
}
}
這是我寫到目前爲止的代碼。 這是結果:
Welcome to MK Supermarket
Four items are available for sale:
1.Flour 2.Crix 3.Juice 4.Cereal
Press 1 for Flour, Press 2 for Crix, Press 3 for Juice, Press 4 for Cereal
2
You chose Flour
在你的代碼中,'input1'將始終爲'1'。你還會期待什麼?你想做什麼? –
@ user2812509在您編寫代碼後,您必須通過它。它只是一個小錯誤 – gjman2