扫描仪输入为何不起作用? [英] Why isn't the scanner input working?

查看:40
本文介绍了扫描仪输入为何不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我是一名新的Java程序员,我试图弄清楚为什么一段代码无法正常工作.我遇到的问题是:字符串兴趣= input.nextLine();",它跳过了用户的输入并跳至下一个System.out,因此它仅在控制台中显示您的个人资料..."在允许用户输入任何数据之前.抱歉,这是一个愚蠢的问题,我很新!

So I am a new Java programmer and I am trying to figure out why a piece of code isn't working. The issue I am having is with the line: "String interests = input.nextLine();", it skips the user's input and jumps to the next System.out, so it just displays "Your profile..." in the console before allowing for the user to input any data. Sorry if it's a dumb question, I'm very new!

System.out.println("Hello, " + name + "! What is your gender?");
String gender = input.nextLine();
System.out.println("Okay, " + name + ", you are a " + gender + ". Now, tell me, what is your age?");
int age = input.nextInt();
System.out.println("Great! We're almost done. What are three interests you have?");
String interests = input.nextLine();

System.out.println("...Your profile...");
System.out.println("Name: " + name);
System.out.println("Gender: " + gender);

推荐答案

按如下所示放置它:

int age = input.nextInt();
input.nextLine();
System.out.println("Great! We're almost done. What are three interests you have?");
String interests = input.nextLine();

其余代码可能与您上面的代码相同.

The rest of the code could be equals to the code that you have above.

编辑:它必须是这样的(我的意思是,不将行存储在您的任何变量中),因为 nextInt()函数不会读取全行,仅是下一个整数.因此,当 nextInt()函数读取 int 时, Scanner 的光标"将位于之后的位置.int .

It has to be like this (I mean, without storing the line in any of your variables) because the nextInt() function don't read the full line, just the next integer. So, when the nextInt() function read the int, the "cursor" of the Scanner will be in the position after the int.

例如,如果您在尝试读取 int 时放了多个单词(用空格隔开),则在您的interest变量中,您将读取 nextInt()之前无法读取.因此,如果您有:

For example, if you put more than one words (separated by a space) when you are trying to reading the int, in your interests variable you will read the rest of the line that the nextInt() couldn't read before. So, if you have:

System.out.println("Okay, " + name + ", you are a " + gender + ". Now, tell me, what is your age?");
//Here you enter --> 18 This is a prove
int age = input.nextInt();
System.out.println("Great! We're almost done. What are three interests you have?");
//Here you won't be able to put anything
String interests = input.nextLine();

现在您将存储:

age = 18;
interests = "This is a prove";

但是,如果您输入如下代码:

But if you put the code like this:

System.out.println("Okay, " + name + ", you are a " + gender + ". Now, tell me, what is your age?");
//Here you enter --> 18 This is a prove
int age = input.nextInt();
input.nextLine();
//Now the Scanner go to a new line (because of the nextLine) but without storing the value
System.out.println("Great! We're almost done. What are three interests you have?");
//Now you are in a new line and you are going to be able to write. For example --> I am reading this.
String interests = input.nextLine();

现在要具有的值是:

age = 18;
interests = "I am reading this.";

因此,总而言之,如果您有一个 nextInt()并尝试读取一个 int ,则可以读取它,但是光标将停留在最后一行,您将无法阅读下一行.为此,您必须读取完整的行而不将其存储在 input.nextLine(); .

So, in conclusion, if you have a nextInt() and you are trying to read a single int you will read it, but the cursor will stay at the final of this line and you won't be able to read the next line. For this, you have to read the full line without storing it with input.nextLine();.

我希望它将对您有所帮助!

I expect it will be helpful for you!

这篇关于扫描仪输入为何不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆