for循环跳过问题

时间:2013-03-24 08:38:00

标签: java arrays for-loop skip

我有这个代码,一切都在运行,因为我想要除了第一次它应该等待用户的下一个输入时跳过的面部。这是代码:

import java.util.Scanner;
public class mainThread {
    public static int size;
    public static int counter;
    public static Scanner input = new Scanner(System.in);
    public static String currentIn;
    public static void main(String[] args){
        System.out.println("Please type the desired amount of players: ");
        size = input.nextInt();
        String nameArray[] = new String[size];
        for(int counter = 0; counter < size; counter++){
            System.out.println("Please enter the name of player " + (counter+1));
            currentIn = input.nextLine();
            nameArray[counter] = currentIn;
            System.out.println(nameArray[counter]);
        }
    }
}

以下是控制台所说的内容:

Please type the desired amount of players: 
3
Please enter the name of player 1

Please enter the name of player 2
Jacob
Jacob
Please enter the name of player 3

它完全跳过第一个循环,我似乎无法弄清楚原因。谢谢你的时间!

4 个答案:

答案 0 :(得分:1)

nextInt()只读取“3”,但不读取换行符。它基本上只是读取第一个令牌分隔符,但不消耗它。所以,如果你输入

3 Foo

然后点击返回,它会以“Foo”作为第一个玩家的名字。

听起来你可能想要在找到玩家数量时使用nextLine()

String playerCountText = input.readLine();
size = Integer.parseInt(playerCountText); // Or use a NumberFormat

就我个人而言,我总是发现Scanner有点像这样烦人 - 它有望使一切变得简单,但你真的需要解决它将要做的所有事情......

答案 1 :(得分:1)

修改程序如下:

import java.util.Scanner;
public class mainThread {
    public static int size;
    public static int counter;
    public static Scanner input = new Scanner(System.in);
    public static String currentIn;
    public static void main(String[] args){
        System.out.println("Please type the desired amount of players: ");
        size = Integer.parseInt(input.nextLine().trim());
        String nameArray[] = new String[size];
        for(int counter = 0; counter < size; counter++){
            System.out.println("Please enter the name of player " + (counter+1));
            currentIn = input.nextLine();
            nameArray[counter] = currentIn;
            System.out.println(nameArray[counter]);
        }
    }
}

问题是\n仍留在缓冲区中,这就是为什么跳过了.nextLine() ..

编辑,以下程序也进行了一些输入验证:

import java.util.Scanner;
public class mainThread {
    public static int size;
    public static int counter;
    public static Scanner input = new Scanner(System.in);
    public static String currentIn;
    public static void main(String[] args){
        boolean valid = false;
        System.out.println("Please type the desired amount of players: ");
        while(valid == false){
         try{
           size = Integer.parseInt(input.nextLine().trim());
           valid = true;
         }catch(Exception e){
            System.out.println("Error input, try again");
         }
        }
        String nameArray[] = new String[size];
        for(int counter = 0; counter < size; counter++){
            System.out.println("Please enter the name of player " + (counter+1));
            currentIn = input.nextLine();
            nameArray[counter] = currentIn;
            System.out.println(nameArray[counter]);
        }
    }
}

答案 2 :(得分:0)

我不确定问题是什么,但是如果你更换了这行

currentIn = input.nextLine();

使用:

currentIn = input.next();

它会起作用。

答案 3 :(得分:0)

基本上是当你输入输入3然后按回车键时,输入计为一个新的行值,它保留在缓冲区中,当你试图在循环中读取一个新行时

currentIn = input.nextLine();

第一次将存储的新行作为输入。 你可以通过添加避免这种情况 currentIn = input.nextLine(); 后 size = input.nextInt();

或从文件中读取输入。

相关问题