如何使用For循环来获取Java中的用户输入?

时间:2014-12-29 15:56:09

标签: java for-loop user-input

我的整个代码发布在下面。我的问题可以在这个输出中看到:

Enter the character representation of your first configuration: p
How many symbols are under this configuration? 3
Enter the first symbol: Enter the first symbol: 1
Enter the first symbol: None
[, 1, None]

正如您所看到的,for循环的第一次迭代是在不获取用户输入的情况下执行的。它似乎已被跳过。

我的问题是这样的:如何创建一个For循环,它将为循环的每次迭代请求用户输入?不跳过我的代码现在正在执行的迭代。我已经在互联网上找到了答案,但我无法找到具体的内容。

import java.util.Scanner;
import java.util.ArrayList;

class read {
    public static void main(String[] args) {

        // Create instance of class
        Tape instructions = new Tape();


        // Assign scanner to keyboard variable
        Scanner keyboard = new Scanner(System.in);


        // Get user input for mConfig and convert to char
        System.out.print("Enter the character representation of your first configuration: ");
        String userMConfig = keyboard.nextLine();
        char c = userMConfig.charAt(0);


        // Get number of symbols/operations/f-configurations
        System.out.print("How many symbols are under this configuration? ");
        int numOfSymbols = keyboard.nextInt();


        // Define array of symbols
        ArrayList<String> userSymbols = new ArrayList<String>();

        for (int i = 1; i<= numOfSymbols; i++)
        {
            System.out.println("Enter the first symbol: ");
            String userInputSymbol = keyboard.nextLine();
            userSymbols.add(userInputSymbol);

        }



        // Assign values to object methods
        instructions.mConfig(c);
        instructions.symbols(userSymbols);



        // Testing code
        System.out.println(instructions.getSymbols());





    }
}

这是我的For循环:

for (int i = 1; i<= numOfSymbols; i++)
            {
                System.out.println("Enter the first symbol: ");
                String userInputSymbol = keyboard.nextLine();
                userSymbols.add(userInputSymbol);

            }

3 个答案:

答案 0 :(得分:4)

当你这样做时

int numOfSymbols = keyboard.nextInt();

扫描程序从输入行获取整数部分,但在缓冲区中留下'\n'。这就是你致电

的原因
String userInputSymbol = keyboard.nextLine();

从循环的第一次迭代开始,'\n'立即返回,产生一个空行。

要解决此问题,请在阅读keyboard.nextLine();后立即添加numOfSymbols

int numOfSymbols = keyboard.nextInt();
keyboard.nextLine(); // Throw away the '\n' from the line that contained int

或者,您可以使用keyboard.nextLine(),然后手动解析int以读取整数。

一般情况下,您应该谨慎地将nextLine与其他Scanner方法的调用混合在一起,正是因为跟踪'\n'个字符的问题。

答案 1 :(得分:3)

这里的问题是nextInt()读取一个int,并在流中留下换行符。然后,调用nextLine()读取此字符并返回空字符串。为避免这种情况,请使用以下内容:

int numOfSymbols = keyboard.nextInt();

keyboard.nextLine(); // this will swallow the "\n"

// Now your for loop should work fine
for (int i = 0; i < numOfSymbols; i++)
{
    System.out.println("Enter the first symbol: ");
    String userInputSymbol = keyboard.nextLine();
    userSymbols.add(userInputSymbol);
}

答案 2 :(得分:1)

这是因为nextInt()不会消耗行尾字符。

你能做什么?你可以在nextInt()电话后手动使用它:

int numOfSymbols = keyboard.nextInt();
...
keyboard.nextLine(); // Consume the end-of-line character

for (int i = 1; i <= numOfSymbols; i++) {
    ...
}

如果您想了解更多信息,可以参考this post

相关问题