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

尝试这个 -

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

Scanner#nextInt将输入的下一个标记扫描为int. 并抛出

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 回答