【问题标题】:User Input not working with keyboard.nextLine() and String (Java)用户输入不适用于 keyboard.nextLine() 和 String (Java)
【发布时间】:2015-04-15 02:41:30
【问题描述】:

我最近在业余时间开始学习 java。所以为了练习,我正在制作一个程序,它需要温度(摄氏度或华氏度)并将其转换为相反的温度。我已经导入了键盘扫描器。

    int temp;
    String opposite, type;
    double product;

    System.out.print("Please enter a temperature: ");
    temp = keyboard.nextInt();

    System.out.println("Was that in Celsius or Fahrenheit?");
    System.out.print("(Enter 'C' for Celsius and 'F' for Fahrenheit) ");
    type = keyboard.nextLine();

    if (type == "C") // Only irrelevant temp conversion code left so I'm leaving it out

我是 String 和 nextLine 的新手,程序只是跳过了输入 C 或 F 的用户输入部分。有人能解释一下我能做些什么来解决这个问题吗?

谢谢!

【问题讨论】:

标签: java


【解决方案1】:

为您的代码将 nextLine(); 更改为 next(); 即可。

System.out.println("Was that in Celsius or Fahrenheit?");
    System.out.print("(Enter 'C' for Celsius and 'F' for Fahrenheit) ");
    type = keyboard.next();

为了让您了解发生的事情是这样的:

  • nextLine():使此扫描仪超过当前行返回被跳过的输入。
  • next():从该扫描器中查找并返回下一个完整的令牌。

也像许多答案说使用equals() 而不是使用==

== 只检查对对象的引用是否相等。 .equal() 比较字符串。

阅读更多Here

【讨论】:

    【解决方案2】:

    切勿在Scanner#nextInt 之后使用Scanner#nextLine。每当您在Scanner#nextInt 之后按回车键时,它将跳过Scanner#nextLine 命令。所以, 变化自

     int temp = keyboard.nextInt();
    

     int temp = Integer.parseInt(keyboard.nextLine());
    

    【讨论】:

      【解决方案3】:

      .nextInt() 不读取行尾字符"\n"

      你需要在.nextInt()之后加上一个keyboard.nextLine(),然后它就可以工作了。

      【讨论】:

        【解决方案4】:

        另外,用type.equals("C")代替if (type == "C"),后面是比较值的引用。

        【讨论】:

          【解决方案5】:

          打电话

            keyboard.nextLine();
          

          之后

            temp = keyboard.nextInt();
          

          因为 nextInt() 不使用 \n 字符。

          另外,比较 Strings.equals(); 而不是 ==

          if(type.equals("C"));
          

          【讨论】:

            【解决方案6】:

            使用扫描器类:

            int temp;
            java.util.Scanner s = new java.util.Scanner(System.in);
            String opposite, type;
            double product;
            
            System.out.print("Please enter a temperature: ");
            temp = s.nextInt();
            
            System.out.println("Was that in Celsius or Fahrenheit?");
            System.out.print("(Enter 'C' for Celsius and 'F' for Fahrenheit) ");
            type = s.nextLine();
            
            if (type.equals("C")){
            do something
            }
            else{
            do something
            }
            

            更多输入参考:

            Scanner

            BufferedReader

            String

            Here 是关于如何在 java 中比较字符串的精彩对比。

            【讨论】:

              【解决方案7】:

              您可以使用keyboard.next() 代替nextLine() 并且作为

              用户1770155

              提到比较两个字符串你应该使用.equals() 既然您要与大写字母“C”进行比较,我会做的是

               type = keyboard.next().toUpperCase();
              
              
              
              if (type.equals("C"))
              

              【讨论】:

                猜你喜欢
                • 2019-03-25
                • 2014-04-11
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2021-01-06
                • 2015-12-02
                • 1970-01-01
                • 2017-08-07
                相关资源
                最近更新 更多