Java 我有一段代码,我必须将IF-ELSE语句合并到其中,但是如果条目不在范围内,如何使其循环?
我只想循环这个代码直到输入正确的答案,有人能教我怎么做吗Java 我有一段代码,我必须将IF-ELSE语句合并到其中,但是如果条目不在范围内,如何使其循环?,java,eclipse,Java,Eclipse,我只想循环这个代码直到输入正确的答案,有人能教我怎么做吗 //enter marital status {Scanner keyboard = new Scanner(System.in); System.out.println("Please enter your Marital Status folowing the numbers given"); System.out.println("0 = Single"); System.out.printl
//enter marital status
{Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter your Marital Status folowing the numbers given");
System.out.println("0 = Single");
System.out.println("1 = Married Filing Jointly or Qualifying Widow(er)");
System.out.println("2 = Married Filing Seperately");
System.out.println("3 = Head of Household");
status = keyboard.nextInt();
{
if (status == 0) {
System.out.println("You have selected single.");}
else if (status == 1) {System.out.println("You have selected Married Filing Seperately or Qualifying Widoe(er).");}
else if (status == 2) {System.out.println("You have selected Married Filing Seperatey");}
else if (status == 3) {System.out.println("You have selected Head of Household");}
else if (status > 3) {System.out.println("Invalid selection"); System.exit(0);}
}
你可以简单地:
boolean valid = false;
while(!valid) {
System.out.println("Please enter your Marital Status folowing the numbers given");
System.out.println("0 = Single");
System.out.println("1 = Married Filing Jointly or Qualifying Widow(er)");
System.out.println("2 = Married Filing Seperately");
System.out.println("3 = Head of Household");
status = keyboard.nextInt();
if(status >= 0 && status <= 3) {
valid = true;
else
System.out.println("Invalid selection");
}
String response = "You have selected: ";
switch(status){
case 0:
response += "single";
break;
case 1:
response += "Married Filing Seperately or Qualifying Widoe(er).";
break;
case 2:
response += "Married Filing Seperatey";
break;
case 3:
response += "You have selected Head of Household";
break;
}
System.out.println(response);
boolean valid=false;
while(!valid){
System.out.println(“请在给出的数字后面输入您的婚姻状况”);
System.out.println(“0=单个”);
System.out.println(“1=已婚共同申报或符合资格的寡妇(er)”;
System.out.println(“2=单独提交);
System.out.println(“3=户主”);
状态=键盘.nextInt();
如果(状态>=0&&status谢谢,但我如何添加一条显示循环是否运行的消息?在高级中再次感谢。没有看到其余内容抱歉,可能此解决方案对您更准确,请查看答案中的编辑。