大家好,我对 Java 还是比较陌生,我只是想知道是否有人可以帮助我限制用户可以输入的次数。正如您现在所看到的,用户可以输入无限次,谁能帮我将其限制为“n”次?
ArrayList<Integer> al = new ArrayList<Integer>();
int check=192;
while(true){
check = input.nextInt();
if(check == 192) break;
al.add(check);
}
for (int i : al) {
System.out.print(i);
}
最佳答案
最简单的方法是在 while 循环之前声明计数器,然后在每次输入后递增计数器。
int count = 0;
int limit = x; // you declare how many times
while(true && count < x){
// your code
count++;
}
关于java - 如何限制数组的输入量? ( java ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42935744/