У меня есть этот код, и все работает, поскольку я хочу, кроме того, что он пропускает первый раз, когда он должен ждать следующего входа пользователя. Вот код:for loop skip issue
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
Он полностью пропускает первый цикл, и я не могу понять, почему. Спасибо за ваше время!
'nextInt()' только считывает значение in, а не остальную часть строки. Вы можете добавить 'input.nextLine();', чтобы игнорировать остальную часть строки. –