【问题标题】:Java util.Scanner not immediately recognizing user inputJava util.Scanner 不能立即识别用户输入
【发布时间】:2018-03-13 11:42:26
【问题描述】:

我对编程完全陌生,我正在尝试用 Java 编写一个非常简单的程序。我希望给用户一个提示,给出两个选项来做什么,然后通过输入一个特定的单词来选择两个选项之一,然后打印出一个指定的消息。

代码如下:

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();    
  }
}

我遇到的问题是,在打印出初始提示后,程序似乎不承认用户输入,除非您输入所需的单词,按回车键,然后按其他字符,然后按回车键。例如,一个选择的单词是“coffee”,但如果我输入“coffee”然后按回车键,除非我输入某个字符然后再次回车,否则什么都不会发生。

根据我所做的研究,我认为我的问题与扫描仪在用户输入所需单词后无法识别返回键有关。为什么这不起作用?

【问题讨论】:

    标签: java java.util.scanner


    【解决方案1】:

    问题出在这几行

    String coffee = input1.next();
      String quit = input1.next();
    

    您的代码需要两个输入。与其将coffeequit 作为两个单独的变量读取,不如读取选项然后执行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");
    } 
    

    【讨论】:

      【解决方案2】:

      这是因为您使用了 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{
      
      }
      

      【讨论】:

        【解决方案3】:

        您的问题是您将两个变量分配给一行输入,coffee 和 quit。所以你只需要去掉String quit = input1.next();这一行,把else if (quite.equals(Quit)) {改成else if (coffee.equals(Quit)) {。这意味着你的输入是咖啡,不管你从他们那里收集到什么输入,它只需要在一个变量中,然后你可以使用该变量来查看它是否等于退出、咖啡或它的无效。

        希望我能帮助你更好地理解它。

        【讨论】:

          【解决方案4】:

          我认为你应该定义一个字符串参数。

          那么你应该使用nextLine() 来获取这样的字符串

          String choices = input1.nextLine();
          

          然后做如果...否则

          【讨论】:

            猜你喜欢
            • 2013-09-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-11-16
            相关资源
            最近更新 更多