为什么我的Java while循环首先进行两次传递?

为什么我的Java while循环首先进行两次传递?,java,arrays,java.util.scanner,Java,Arrays,Java.util.scanner,为什么我的while循环在允许用户输入之前要经过两次?在for循环中尝试了相同的方法,但无法理解为什么在允许用户输入输入之前,它会两次请求输入。我知道这是一个愚蠢的,简单的逻辑错误,但我是个傻瓜。提前谢谢 public static void main (String [] args){ Scanner scan = new Scanner(System.in); System.out.println("How large would you like the array t

为什么我的while循环在允许用户输入之前要经过两次?在for循环中尝试了相同的方法,但无法理解为什么在允许用户输入输入之前,它会两次请求输入。我知道这是一个愚蠢的,简单的逻辑错误,但我是个傻瓜。提前谢谢

public static void main (String [] args){

    Scanner scan = new Scanner(System.in);

    System.out.println("How large would you like the array to be? (number)");
    int arraySize = scan.nextInt();
    String [] myArray = new String [arraySize];
    int i = 0;

    if (arraySize <= 0 ) {
        System.out.println("Please enter a positive integer for the array size. Rerun program when ready.");
    } else {
        while (i < myArray.length) {
            System.out.println("Please type a string to be entered in the array");
            myArray[i] = scan.nextLine();
            i++;
        }
    }


}
添加
scan.nextLine()就在您的
nextInt
之后:

int arraySize = scan.nextInt();
scan.nextLine();
while
循环的第一次迭代在
nextLine()
上没有阻塞,因为它在您输入的第一个整数之后拾取新行。

Add
scan.nextLine()就在您的
nextInt
之后:

int arraySize = scan.nextInt();
scan.nextLine();
while
循环的第一次迭代没有阻塞
下一行()
,因为它在您输入的第一个整数之后拾取新行。

尝试以下操作:

public static void main (String [] args){

    Scanner scan = new Scanner(System.in);

    System.out.println("How large would you like the array to be? (number)");
    int arraySize = scan.nextInt();
    scan.nextLine(); // This advances the Scanner to the next line.
    String [] myArray = new String [arraySize];
    int i = 0;

    if (arraySize <= 0 ) {
        System.out.println("Please enter a positive integer for the array size. Rerun program when ready.");
    } else {
        while (i < myArray.length) {
            System.out.println("Please type a string to be entered in the array");
            myArray[i] = scan.nextLine();
            i++;
        }
    }


}
publicstaticvoidmain(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
System.out.println(“您希望数组有多大?(数字)”;
int arraySize=scan.nextInt();
scan.nextLine();//这将使扫描仪前进到下一行。
String[]myArray=新字符串[arraySize];
int i=0;
如果(arraySize尝试以下操作:

public static void main (String [] args){

    Scanner scan = new Scanner(System.in);

    System.out.println("How large would you like the array to be? (number)");
    int arraySize = scan.nextInt();
    scan.nextLine(); // This advances the Scanner to the next line.
    String [] myArray = new String [arraySize];
    int i = 0;

    if (arraySize <= 0 ) {
        System.out.println("Please enter a positive integer for the array size. Rerun program when ready.");
    } else {
        while (i < myArray.length) {
            System.out.println("Please type a string to be entered in the array");
            myArray[i] = scan.nextLine();
            i++;
        }
    }


}
publicstaticvoidmain(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
System.out.println(“您希望数组有多大?(数字)”;
int arraySize=scan.nextInt();
scan.nextLine();//这将使扫描仪前进到下一行。
String[]myArray=新字符串[arraySize];
int i=0;

if(arraySize nextLine()可能会在缓冲区中留下一个字符,因此nextLine()会立即检查
myArray[0]
是否为5。@Ben非常接近。你忘记了由
Scanner\nextLine
读取的换行字符。我知道是这样的。我讨厌用户输入:)另外,在执行donenextInt()时调用scan.close()的一个好方法可能会在缓冲区中留下一个字符,因此nextLine()检查是否
myArray[0]会立即提取该字符
是5。@Ben非常接近。你忘记了扫描仪#nextLine
读取的换行符。我知道是这样的。Gah我讨厌用户输入:)完成后调用scan.close()也是一个很好的做法