用户输入不适用于 keyboard.nextLine() 和 String (Java)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29640718/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:38:59  来源:igfitidea点击:

User Input not working with keyboard.nextLine() and String (Java)

java

提问by js9999

I recently started learning java during my spare time. So to practice, I'm making a program that takes a temperature (Celsius or Fahrenheit) and converts it to the opposite. I've already imported the keyboard scanner.

我最近在业余时间开始学习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

I'm new to the String and nextLinestuff and the program just skips over the user input section where you enter either C or F. Would someone explain what I can do to fix this?

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

Thanks!

谢谢!

采纳答案by Anjula Ranasinghe

For you code Change nextLine();to next();and it will work.

对于您的代码更改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();

to get an idea for you to what happened was this:

让你知道发生了什么是这样的:

  • nextLine(): Advances this scanner past the current lineand returns the input that was skipped.
  • next(): Finds and returns the next complete token from this scanner.
  • nextLine():将此扫描器推进到当前行返回被跳过的输入。
  • next():从这个扫描器中查找并返回下一个完整的标记。

Also like the many of the answers says use equals()instead of using ==

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

The ==checks only the references to the object are equal. .equal()compares string.

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

Read more Here

在这里阅读更多

回答by beatyt

.nextInt()does not read the end of line character "\n".

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

You need to put a keyboard.nextLine()after the .nextInt()and then it will work.

你需要把一keyboard.nextLine()后的.nextInt(),然后它会奏效。

回答by Masudul

Never use Scanner#nextLineafter Scanner#nextInt. Whenever you hit enter button after Scanner#nextIntthan it will skip the Scanner#nextLinecommand. So, Change from

以后再也不用Scanner#nextLineScanner#nextInt。每当您按下回车按钮后Scanner#nextInt,它都会跳过该Scanner#nextLine命令。所以,从

 int temp = keyboard.nextInt();

to

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

回答by PWC

Also, use type.equals("C")instead of if (type == "C"), the later one is comparing the reference of the value.

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

回答by JClassic

Call

称呼

  keyboard.nextLine();

After

  temp = keyboard.nextInt();

Because nextInt() doesn't consume the \ncharacter.

因为 nextInt() 不消耗\n字符。

Also, compare Stringswith .equals();not ==

此外,Strings.equals();不比较==

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

回答by vishu9219

Use Scanner Class:

使用扫描仪类:

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
}

For More Input references:

更多输入参考:

Scanner

扫描器

BufferedReader

缓冲阅读器

String

细绳

Hereis a wonderful comparison on how to compare strings in java.

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

回答by STIKO

You could use keyboard.next()instead of nextLine()and as

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

user1770155

用户1770155

mentioned to compare two strings you should use .equals()and what I would do since you're comparing to an upper letter "C" is

提到比较您应该使用的两个字符串.equals(),以及我会做的事情,因为您要与大写字母“C”进行比较

 type = keyboard.next().toUpperCase();



if (type.equals("C"))