我是java和学习数组的新手,所以我为我的问题标题中确定的错误术语道歉,但我如何强迫用户在数组中输入多个值,还要不允许他们进入超过,比如90?
答案 0 :(得分:0)
也许这就是你所期待的do while example
import java.util.Scanner;
class Demo {
public static void main(String[] args) {
int [] Ages = new int[90]; // Set the size of the array
Scanner scan = new Scanner(System.in);
int counter = 0;
do {
try
{
int number = scan.nextInt(); // Scan the number
Ages[ counter ] = number; // Storage in the array
} catch (Exception e) {
System.out.println("Only numbers are allowed"); // only allows number
counter--;
}
counter++;
} while ( counter <= 89 ); // you need you enter 90 number
}
}
我希望这可以回答你的问题。