首先你想用你Scanner的从键盘上阅读。您已经完成了一半:
Scanner userInputReader = new Scanner(System.in);
String userInput = userInputReader.nextLine();
您可以简单地检查是否userInput等于yes/no像这样:
if(userInput.equals("yes")){ //note strings are compared with .equals, not ==
//"yes" case
}else if(userInput.equals("no")){
//"no" case
}else{
//neither "yes" nor "no"
}
或者,switch语句也可以使用
switch(userInput){
case "yes":
//yes case
break;
case "no":
//no case
break;
default:
//neither "yes" nor "no"
break;
}
如果给出无效输入,则使其要求更多输入:
while(true){
String userInput = userInputReader.nextLine();
if(userInput.equals("yes")){ //note strings are compared with .equals, not ==
//"yes" case
//generate your numbers for "yes"
break;
}else if(userInput.equals("no")){
//"no" case
//generate your numbers for "no"
break;
}else{
//neither "yes" nor "no"
//note that the continue statement is redundant and
//the whole else-block can be omitted
continue;
}
}