从标准输入中获取输入

miatech:

我想从的标准输入

3
10 20 30

第一个数字是第二行中的数字量。这就是我得到的,但是它被困在while循环中...所以我相信。我在调试模式下运行,并且数组未分配任何值...

import java.util.*;

public class Tester {   

   public static void main (String[] args)
   {

       int testNum;
       int[] testCases;

       Scanner in = new Scanner(System.in);

       System.out.println("Enter test number");
       testNum = in.nextInt();

       testCases = new int[testNum];

       int i = 0;

       while(in.hasNextInt()) {
           testCases[i] = in.nextInt();
           i++;
       }

       for(Integer t : testCases) {
           if(t != null)
               System.out.println(t.toString());               
       }

   } 

} 
mu_sa:

它与条件有关。

in.hasNextInt()

它使您可以继续循环,然后在3次迭代之后,“ i”值等于4,并且testCases [4]引发ArrayIndexOutOfBoundException。

解决此问题的方法可能是

for (int i = 0; i < testNum; i++) {
 *//do something*
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章