if语句如何使if条件在Java中重复?
基本上,我希望能够输入一个不是选项的数字,然后被赋予再次选择的选项,并让语句重复
Scanner keyboard = new Scanner(System.in);
double weight;
int Choice;
System.out.println("What is your weight in pounds?");
weight = keyboard.nextDouble();
System.out.println("Which planet would you like to see your weight on?\n 1. Venus 2. Mars 3. Jupiter\n 4. Saturn 5. Uranus 6. Neptune");
Choice =keyboard.nextInt();
if (Choice == 1){
System.out.println("Your weight on Venus would be " + (weight * 0.78));
}
else if (Choice == 2){
System.out.println("Your weight on Mars would be " + (weight * .39));
}
else if (Choice == 3){
System.out.println("Your weight on Jupiter would be " + (weight * 2.65));
}
else if (Choice == 4){
System.out.println("Your weight on Saturn would be " + (weight * 1.17));
}
else if (Choice == 5){
System.out.println("Your weight on Uranus would be" +(weight * 1.05));
}
else if (Choice == 6) {
System.out.println("Your weight on Neptune would be " + (weight * 1.23));
}
else
System.out.println("This was not a choice, try again!");
Choice = keyboard.nextInt();
}
# 1 楼答案
你必须学习编程语言的基本知识做。。而和切换语句
下面是示例程序
使用Java命名约定为变量命名。以小写字母开头
# 2 楼答案
这是一种更简单的方法,使用
do-while
循环和switch
也是固定的选择
# 3 楼答案
这可以通过while循环完成。也可以考虑使用数组。
所以,基本上,你礼貌地问一次。之后,开始大喊这是错误的,直到用户给出正确的输入