java java循环,如果否则
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15489511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java loop, if else
提问by user2044988
I know this is something very simple, however, I've only been programming for a couple months so my brain just fog's out sometimes, and I need help with a while loop with nested else if statements. The problem my loop is continuous because the user is never given the chance to enter in their choice (which -1 stops the loop). How can I change the loop so that it operates by asking user to enter a choice (1-4, or -1 to quit)
我知道这是非常简单的事情,但是,我只编程了几个月,所以我的大脑有时会迷糊,我需要帮助处理带有嵌套 else if 语句的 while 循环。我的循环问题是连续的,因为用户从来没有机会输入他们的选择(-1 停止循环)。如何通过要求用户输入选择(1-4 或 -1 退出)来更改循环以使其运行
Please Any help is appreciated. I know it's something simple, I've searched thorugh previous forum discussions but I can't seem to make it work.
请任何帮助表示赞赏。我知道这很简单,我已经通过以前的论坛讨论进行了搜索,但似乎无法使其正常工作。
//create new scanner object
Scanner userInput = new Scanner (System.in);
int end = 0;
//find out what user wants to do with address book
while (end != -1)
{
System.out.println(" ");
System.out.println(" ");
System.out.println("----------------------------");
System.out.println("What would you like to do with your address book? ...");
System.out.println("----------------------------");
System.out.println("Add new [Enter 1]");
System.out.println("Delete existing [Enter 2]");
System.out.println("Edit existing [Enter 3]");
System.out.println("Search for [Enter 4]");
System.out.println("EXIT [Enter -1]");
}
if (userInput.hasNext() && (userInput.nextInt() == 1))
{
AddressBookProcessor.addContact();
}
else if (userInput.hasNext() && userInput.nextInt() == 2)
{
AddressBookProcessor.deleteContact();
}
else if (userInput.hasNext() && userInput.nextInt() == 3)
{
AddressBookProcessor.editContact();
}
else if (userInput.hasNext() && userInput.nextInt() == 4)
{
AddressBookProcessor.findContact();
}
else if (userInput.nextInt() != 1 || userInput.nextInt() != 2
|| userInput.nextInt() != 3 || userInput.nextInt() != -1)
{
System.out.println("Please enter a valid input");
end = -1;
}
}
回答by Bohemian
Move your if/else's inside the while loop.
将 if/else 移动到 while 循环中。
回答by 0x6C38
Simply putting the if statement inside the loop should work (note I haven't checked anything else on the code this is just a quick response):
简单地将 if 语句放在循环中应该可以工作(注意我没有检查代码上的任何其他内容,这只是一个快速响应):
//create new scanner object
Scanner userInput = new Scanner (System.in);
int end = 0;
//find out what user wants to do with address book
while (end != -1)
{
System.out.println(" ");
System.out.println(" ");
System.out.println("----------------------------");
System.out.println("What would you like to do with your address book? ...");
System.out.println("----------------------------");
System.out.println("Add new [Enter 1]");
System.out.println("Delete existing [Enter 2]");
System.out.println("Edit existing [Enter 3]");
System.out.println("Search for [Enter 4]");
System.out.println("EXIT [Enter -1]");
if (userInput.hasNext() && (userInput.nextInt() == 1))
{
AddressBookProcessor.addContact();
}
else if (userInput.hasNext() && userInput.nextInt() == 2)
{
AddressBookProcessor.deleteContact();
}
else if (userInput.hasNext() && userInput.nextInt() == 3)
{
AddressBookProcessor.editContact();
}
else if (userInput.hasNext() && userInput.nextInt() == 4)
{
AddressBookProcessor.findContact();
}
else if (userInput.nextInt() != 1 || userInput.nextInt() != 2
|| userInput.nextInt() != 3 || userInput.nextInt() != -1)
{
System.out.println("Please enter a valid input");
end = -1;
}
}
}
回答by rgettman
You don't change the end
variable inside the while
loop, so that results in an infinite loop. You will need to place your input processing logic inside the while loop so that end
has a chance to change, so that the while
loop can end.
您不会更改循环end
内的变量while
,因此会导致无限循环。您需要将输入处理逻辑放在 while 循环中,以便end
有机会更改,以便while
循环可以结束。