在 Java 中提示用户是或否输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23047309/
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
Prompt for user yes or no input in Java
提问by user3443401
how to prompt user to loop the code yes to loop no to exit and wrong input print wrong input and go back to statement ie. "do you want to enter another name:"
如何提示用户循环代码是循环否退出和错误输入打印错误输入并返回语句即。“要不要输入另一个名字:”
import java.util.Scanner;
public class loop {
public static void main(String[] args){
Scanner kbd = new Scanner (System.in);
String decision;
boolean yn;
while(true){
System.out.println("please enter your name");
String name = kbd.nextLine();
System.out.println("you entered the name" + name );
System.out.println("enter another name : yes or no");
decision = kbd.nextLine();
switch(decision){
case "yes":
yn = false;
break;
case "no":
yn = true;
break;
default :
System.out.println("please enter again ");
return default;
}
}
}
}
采纳答案by Marco Acierno
- If you don't use Java 7 you can't use switch-strings
Change
while (true)
withwhile (yn)
so it will stop when he type "no", and changeboolean yn;
toboolean yn = true;
And change the rules inside the cases too.case "yes": yn = false; break; case "no": yn = true; break;
yn = true;
if"yes"
;yn = false;
if"no"
;You could change the condition inside the while, with
while (!yn)
but is more intuitive to letyn
true
if yes;false
if no.return default;
don't make much sense, if you want to let user repeat in case of error well.. you should do a newwhile (true)
to repeat until he writes a correct one. I would write another method.
- 如果您不使用 Java 7,则不能使用开关字符串
更改
while (true)
为while (yn)
所以当他输入“否”时它会停止,并更改boolean yn;
为boolean yn = true;
并更改案例内的规则。case "yes": yn = false; break; case "no": yn = true; break;
yn = true;
如果"yes"
;yn = false;
如果"no"
;您可以更改 while 内的条件,如果是,则使用
while (!yn)
但更直观yn
true
;false
如果不。return default;
没有多大意义,如果你想让用户在错误的情况下重复......你应该做一个新while (true)
的重复,直到他写出一个正确的。我会写另一种方法。
This is how you could do it
这就是你可以做到的
Scanner kbd = new Scanner (System.in);
String decision;
boolean yn = true;
while(yn)
{
System.out.println("please enter your name");
String name = kbd.nextLine();
System.out.println("you entered the name" + name );
System.out.println("enter another name : yes or no");
decision = kbd.nextLine();
switch(decision)
{
case "yes":
yn = true;
break;
case "no":
yn = false;
break;
default:
System.out.println("please enter again ");
boolean repeat = true;
while (repeat)
{
System.out.println("enter another name : yes or no");
decision = kbd.nextLine();
switch (decision)
{
case "yes":
yn = true;
repeat = false;
break;
case "no":
yn = repeat = false;
break;
default:
repeat = true;
}
}
break;
}
}
Yes it will repeat decision
code, but how it is created i think it is the only way to do it.
是的,它会重复decision
代码,但它是如何创建的,我认为这是唯一的方法。
回答by AstroCB
Yes, but you'd want while(yn)
, not while(true)
, which goes on forever. The break
only breaks from the switch
statement.
是的,但您想要while(yn)
,而不是while(true)
,这将永远持续下去。在break
从唯一休息switch
的语句。
Alright, try this:
好的,试试这个:
import java.util.Scanner;
public static void main(String args[]){
Scanner s = new Scanner(System.in);
boolean checking = true, valid = true;
String[] names = new String[50];
int i = 0;
while(checking){
System.out.println("Enter name...");
me = s.nextLine();
System.out.println("You entered " + me + ".");
while(valid){
System.out.println("Enter another? y/n");
you = s.nextLine();
if(you.equals("n")){
valid = false;
checking = false;
}else if you.equals("y")){
names[i] = you;
i++;
valid = false;
}else{
System.out.println("Sorry, try again (y/n)...");
}
}
}
}
回答by dev2d
boolean input = true;
while(input){
//ask for name
//print message saying you entered xyz
//ask if user wants to enter other name
//if user enters no
//set input = false;
//else continue
}
try doing this
尝试这样做
回答by jedison
Rather then break and while(true), you need to say while(!yn)
as you set yn to false when the user input yes.
而不是 break 和 while(true),您需要while(!yn)
在用户输入 yes 时将 yn 设置为 false 时说。
回答by v0rin
I just created a small class YesNoCommandPromptthat does just that:
我刚刚创建了一个小类YesNoCommandPrompt来做到这一点:
private String prompt;
private Supplier<T> yesAction;
private Supplier<T> noAction;
public YesNoCommandPrompt(String prompt, Supplier<T> yesAction, Supplier<T> noAction) {
this.prompt = prompt;
this.yesAction = yesAction;
this.noAction = noAction;
}
// example usage
public static void main(String[] args) {
YesNoCommandPrompt<String> prompt = new YesNoCommandPrompt<>(
"Choose",
() -> {return "yes";},
() -> {return "no";});
System.out.println(prompt.run());
}
public T run() {
final String yesOption = "y";
final String noOption = "n";
try (Scanner scanner = new Scanner(System.in)) {
while (true) {
System.out.print(prompt + " [" + yesOption + "/" + noOption + "]: ");
String option = scanner.next();
if (yesOption.equalsIgnoreCase(option)){
return yesAction.get();
}
else if (noOption.equalsIgnoreCase(option)){
return noAction.get();
}
}
}
}