以下はコードです。配列を入力した後、コンソールはちょうど空白になり、さらに出力に配列されていません。スキャナー使用時に配列値が出力されない
import java.util.Scanner;
public class advancedArrays {
public static void main(String[] args) {
System.out.println("Provide us the size of the array:");
Scanner scanner = new Scanner(System.in);
int value = scanner.nextInt();
int i = 0;
int[] array = new int[value];
System.out.println("Enter the array:");
Scanner input = new Scanner(System.in);
while(input.hasNextInt()) {
array[i] = input.nextInt();
i++;
}
System.out.println("Array entered:");
for(i=0;i<value;i++)
{
System.out.println(array[i]);
}
input.close();
scanner.close();
}
}
出力:
Provide us the size of the array:
5
Enter the array:
1 2 3 4 5
私は、while(input.hasNextInt())によってアプリケーションが6番目のintを待っていると強く想定しています。とにかくユーザーが配列のサイズを入力できるようにするので、何度もループするのはなぜですか?ユーザーはint型ではない入力を処理する必要がありますが、現在は範囲外で処理することを検討します。 – Thomas
複数のスキャナを作成しないでください。 –