简体   繁体   English

如何使Java等待用户输入?

[英]How to make java wait for user input?

So, I have a loop that uses the Scanner to input 3 separate pieces of user input, 2 ints and a String. 因此,我有一个循环,它使用扫描器输入3个单独的用户输入,2个整数和一个字符串。 The ints work just fine, but the String input is not working. 整数工作正常,但字符串输入无效。 It skips over the iput for the String, and just loops around to ask for the int again. 它跳过了String的iput,只是循环循环以再次请求int。

for(int e = 10; e > 5; e++)
    {
        System.out.println("Enter the Y coordinate of your guess. The upper left corner is 0, and the lower left is 7");
        int guessY = inputDevice.nextInt();
        System.out.println("Enter the X coordinate of your guess. The upper left corner is 0, and the upper right is 7");
        int guessX = inputDevice.nextInt();

        display[guessX][guessY] = map[guessX][guessY];
        for(int j = 0; j < 8; j++)

                {

            for(int t = 0; t < 8; t++)

            {

                System.out.print(display[j][t] + " ");

            }

            System.out.println();

        }


        System.out.println("Enter go when you are ready to make another move.");
        System.out.println("Enter quit to give up");
        String cont = inputDevice.nextLine();

        if(cont.equalsIgnoreCase("quit"))
        {
            System.out.println("Weenie.");
            System.exit(0);
        } else if(cont.equalsIgnoreCase("go")) {
            for(int i = 0; i < 100; i++)
                System.out.println("\b");
        }

        if(map[guessX][guessY] == '0')
        {
            if(map[guessX - 1][guessY - 1] == '0')
                display[guessX - 1][guessY - 1] = map[guessX - 1][guessY - 1];
            if(map[guessX - 1][guessY + 0] == '0')
                display[guessX - 1][guessY + 0] = map[guessX - 1][guessY + 0];
            if(map[guessX - 1][guessY + 1] == '0')
                display[guessX - 1][guessY + 1] = map[guessX - 1][guessY + 1];
            if(map[guessX + 0][guessY - 1] == '0')
                display[guessX + 0][guessY - 1] = map[guessX + 0][guessY - 1];
            if(map[guessX + 0][guessY + 1] == '0')
                display[guessX + 0][guessY + 1] = map[guessX + 0][guessY + 1];
            if(map[guessX + 1][guessY - 1] == '0')
                display[guessX + 1][guessY - 1] = map[guessX + 1][guessY - 1];
            if(map[guessX + 1][guessY + 0] == '0')
                display[guessX + 1][guessY + 0] = map[guessX + 1][guessY + 0];
            if(map[guessX + 1][guessY + 1] == '0')
                display[guessX + 1][guessY + 1] = map[guessX + 1][guessY + 1];
        }
        if(display[guessX][guessY] == 'M')
        {
            System.out.println("You hit a mine");
            System.out.println("Game over.");
            System.exit(0);
        }
    }

From java 7 documentation http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html : 从Java 7文档http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

public String nextLine() 公共字符串nextLine()
Advances this scanner past the current line and returns the input that was skipped. 使该扫描仪前进到当前行之外,并返回被跳过的输入。 This method returns the rest of the current line, excluding any line separator at the end. 此方法返回当前行的其余部分,但不包括最后的任何行分隔符。 The position is set to the beginning of the next line. 该位置设置为下一行的开始。

Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present. 由于此方法继续在输入中搜索以寻找行分隔符,因此,如果不存在行分隔符,它可以缓冲所有要搜索的输入以跳过该行。

You probably wanted to use: 您可能想使用:

public String next() 公共字符串next()
Finds and returns the next complete token from this scanner. 查找并返回此扫描仪的下一个完整令牌。 A complete token is preceded and followed by input that matches the delimiter pattern. 完整的标记在其前面,然后是与定界符模式匹配的输入。 This method may block while waiting for input to scan, even if a previous invocation of hasNext() returned true. 即使先前调用hasNext()返回true,此方法也可能在等待输入扫描时阻塞。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM