我希望从for中获取stdin的输入
3
10 20 30
Run Code Online (Sandbox Code Playgroud)
第一个数字是第二行中的数字量.这就是我得到的东西,但它停留在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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
Liv*_*ing 10
这与病情有关.
in.hasNextInt()
Run Code Online (Sandbox Code Playgroud)
它允许你保持循环,然后在三次迭代后'i'值等于4并且testCases [4]抛出ArrayIndexOutOfBoundException.
解决方案可能是这样的
for (int i = 0; i < testNum; i++) {
*//do something*
}
Run Code Online (Sandbox Code Playgroud)