English 中文(简体)
简 报 辛勤法规不发挥作用[复制]
原标题:Simple Java Scanner code not working [duplicate]

Here is the skeleton of some basic code I am writing to make a simple game:

    Scanner in = new Scanner(System.in);

    String name;
    String playing;
    int age;

    do {

        System.out.println("Enter your name");
        name = in.nextLine();

        System.out.println("Enter your age");
        age = in.nextInt();

        System.out.println("Play again?");
        playing = in.nextLine();

    } while (true);

举例来说,《守则》并不像预期的那样发挥作用,这里就是《守则》的预期运作:

Enter your name
John
Enter your age
20
Play again?
Yes
Enter your name
Bill
...

然而,还有一个问题是“游戏”再行读,这是实际产出:

Enter your name
John
Enter your age
20
Play again?
Enter your name

As you can see "Enter your name" is being displayed again before "Play again?" is able to accept input. When debugging the playing variable is set to "", so there is no input that I can see and I cannot figure out what is being consumed. Any help would be appreciated, thanks!

最佳回答

<代码>nextInt( >tt 消费线末,即使int<>/code>是唯一的。

在阅读int后添加另一个nextLine(>>,要么完全放弃其价值,要么核查如果你想要阻止人们进入任何东西,那是空洞的。

问题回答

The problem is that after calling nextInt() there is still a in the buffer and so that is what is passed. use in.next() instead of in.nextLine().

使用next()法,而不是使用下限法。

    Scanner in = new Scanner(System.in);

    String name;
    String playing;
    int age;

    do {

        System.out.println("Enter your name");
        name = in.next();

        System.out.println("Enter your age");
        age = in.nextInt();

        System.out.println("Play again?");
        playing = in.next();

    } while (true);

for more information refer Java Documentation for Scanner class Output :

Enter your name
Kanishka
Enter your age
23
Play again?
Yes
Enter your name
....

你们应当使用以下法典进行下行读。

Scanner scanner = new Scanner(System.in).useDelimiter("
");

读一下,请写

scanner.next();




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签