0

私は初心者です...我慢してください:)

これは、配列内の10個の整数を読み取ると思われるコードです。charのInputStreamReader読み取り(java Docsが言ったこと)charをintに変換して配列に保存する方法は?

int[] array = new int[10];
System.out.println("Please enter 10 integers. ");
for(int x=1; x <11; x++){
    InputStreamReader keyboard = new InputStreamReader(System.in);
    // Change char into int   
    array[x] = keyboard;
}

ありがとう

4

1 に答える 1

4

Try this -

Scanner sc = new Scanner(System.in);
int[] array = new int[10];
for(int x=0; x <10; x++){
    array[x] = sc.nextInt();
}

Scanner#nextInt scans the next token of the input as an int. And throws

InputMismatchException - if the next token does not match the Integer regular expression, or is out of range
NoSuchElementException - if input is exhausted
IllegalStateException - if this scanner is closed
于 2012-12-21T11:00:00.310 に答える