我正在尝试构建一个通过扫描程序构建的整数数组列表。用户必须输入 5 到 10 个整数才能使 ArrayList 有效,但在程序运行时,实际输入数将是未知的。
如何构建程序,使其仅在用户输入 5、6、7、8、9 或 10 个整数时运行?
用户应该在一行上输入所有整数,但是我下面尝试的代码在第5个整数之后将其切断,即使后面还有更多整数
public static ArrayList arrayListBuilder() {
Scanner input = new Scanner(System.in);
ArrayList<Integer> integerList = new ArrayList();
System.out.println("Enter 5-10 ints");
while (input.hasNextInt()) {
integerList.add(input.nextInt());
if (integerList.size() > 4 && integerList.size() < 9) {
break;
}
}
return integerList;
}
谢谢!
慕田峪9158850
SMILET
慕姐4208626
相关分类