javautil.Scanner无法立即识别用户输入
我对编程完全陌生,我正在尝试用Java编写一个非常简单的程序。我希望给用户一个提示,给用户两个选项,然后通过键入一个特定的单词从两个选项中选择一个,然后打印出一条指定的消息 代码如下:javautil.Scanner无法立即识别用户输入,java,java.util.scanner,Java,Java.util.scanner,我对编程完全陌生,我正在尝试用Java编写一个非常简单的程序。我希望给用户一个提示,给用户两个选项,然后通过键入一个特定的单词从两个选项中选择一个,然后打印出一条指定的消息 代码如下: import java.util.Scanner; public class coffeeProgram { public static void main(String[] args) { String Quit; String Coffee; Quit = "quit";
import java.util.Scanner;
public class coffeeProgram {
public static void main(String[] args) {
String Quit;
String Coffee;
Quit = "quit";
Coffee = "coffee";
System.out.println("You are sitting at your cubicle, staring at your computer screen.");
System.out.println("What do you want to do?");
System.out.println("Options: a) type 'coffee' to get a cup of coffee; or b) type 'quit' to quit your job");
System.out.println("Enter an action:");
Scanner input1 = new Scanner(System.in);
String coffee = input1.next();
String quit = input1.next();
if (coffee.equals(Coffee)) {
System.out.println("You stand up, and start walking to the coffee machine.");
}
else if (quit.equals(Quit)) {
System.out.println("You walk into your boss's office and say I QUIT");
}
else {
System.out.println("That's not a valid command");
}
input1.close();
}
}
我遇到的问题是,在打印出初始提示后,程序似乎不会确认用户输入,除非您输入所需的单词,点击return,然后点击其他字符,然后再点击return。例如,一个选项中的单词是“coffee”,但如果输入“coffee”并后跟return键,则除非再次输入某个字符并后跟return,否则不会发生任何事情
根据我所做的研究,我认为我的问题与扫描仪在用户输入所需单词后无法识别返回键有关。为什么这样不行?问题就在这些方面
String coffee = input1.next();
String quit = input1.next();
您的代码需要两个输入。与其将coffee
和quit
作为两个单独的变量来阅读,您应该先阅读选项,然后执行if..else
差不多
String choice = input1.next();
if (choice.equals(Coffee)) {
System.out.println("You stand up, and start walking to the coffee machine.");
}
else if (choice.equals(Quit)) {
System.out.println("You walk into your boss's office and say I QUIT");
}
问题就在这些方面
String coffee = input1.next();
String quit = input1.next();
您的代码需要两个输入。与其将coffee
和quit
作为两个单独的变量来阅读,您应该先阅读选项,然后执行if..else
差不多
String choice = input1.next();
if (choice.equals(Coffee)) {
System.out.println("You stand up, and start walking to the coffee machine.");
}
else if (choice.equals(Quit)) {
System.out.println("You walk into your boss's office and say I QUIT");
}
你的问题是你给一行输入分配了两个变量,coffee和quit。所以,您只需要去掉
字符串quit=input1.next()
line并将else if(quite.equals(Quit)){
更改为else if(coffee.equals(Quit)){
。所有这一切意味着你的输入是在coffee中,不管你从他们那里收集了什么输入,它只需要在一个变量中,然后你可以使用该变量来查看它是否等于Quit、coffee或它是否无效
希望我能帮助你更好地理解它。你的问题是你给一行输入赋值了两个变量,coffee和quit。所以你只需要去掉
字符串quit=input1.next();
行并将else if(quite.equals(quit)){/code>改为else if(coffee.equals(quit)){
。所有这一切意味着你的输入是在咖啡中,不管你从他们那里收集了什么输入,它只需要在一个变量中,然后你可以使用该变量来查看它是否等于退出、咖啡或者它是否无效
希望我能帮助您更好地理解它。我认为您应该定义一个字符串参数
然后您应该使用nextLine()
来获取如下字符串
String choices = input1.nextLine();
然后做if…else我认为您应该定义一个字符串参数
然后您应该使用nextLine()
来获取如下字符串
String choices = input1.nextLine();
然后做if…else这是因为您使用了两次input1.next();
//create Scanner instance
Scanner input1 = new Scanner(System.in);
//wait NEXT input
String coffee = input1.next();
//wait another NEXT input
String quit = input1.next();
程序正在等待两个输入,然后才能给您一些响应
所以,正确的方法应该是这样
//create Scanner instance
Scanner input1 = new Scanner(System.in);
//wait NEXT one input
String command = input1.next();
//compare input with predefined commands
if(command.equals(Coffee)){
}else if(command.equals(Quit)){
}else{
}
这是因为您使用了input1.next();两次
//create Scanner instance
Scanner input1 = new Scanner(System.in);
//wait NEXT input
String coffee = input1.next();
//wait another NEXT input
String quit = input1.next();
程序正在等待两个输入,然后才能给您一些响应
所以,正确的方法应该是这样
//create Scanner instance
Scanner input1 = new Scanner(System.in);
//wait NEXT one input
String command = input1.next();
//compare input with predefined commands
if(command.equals(Coffee)){
}else if(command.equals(Quit)){
}else{
}