Suppose I want to limit the input numbers to 20 in the following program, what should I do?
private static void Question2(){
Scanner scr = new Scanner(System.in);
int count = 0;
System.out.println("How many numbers are you going to enter (not greater than 20)");
int n = scr.nextInt();
int[] num = new int[n]; // num is an array of n integers
System.out.printf("Enter %d numbers\n", n);
for (int i = 0; i < num.length; i++) {
num[i] = scr.nextInt();
}
System.out.println("Pick a number");
int x = scr.nextInt();
for (int i = 0; i < num.length; i++) {
if(num[i] == x) {
count++;
}
}
System.out.printf("You entered %d occurence of %ds\n", count , x);
}