嗨,当用户输入某个数字时,我试图输出一行。但是,即使您输入另一个数字,同一行也会不断出现。我究竟做错了什么?
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