Create a program using two-dimensional arrays that searches a number and displays the number of times it occurs on the list of 12 input values.
Sample input/output dialogue:
Enter twelve numbers:
15 20 13 30 35 40 16 18 20 18 20
Enter a number to search: 20
Occurrence(s) : 3
import java.util.Scanner;
public class Answer {
public static void main(String[] args) {
int[][] array = new int[12] [1];
System.out.println("Enter twelve numbers: ");
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < array.length; i++) {
array[i][0] = scanner.nextInt();
}
System.out.print("Enter a number to search:");
int matcher = scanner.nextInt();
int ans = 0;
for (int[] ints : array) {
if (matcher == ints[0]) {
ans++;
}
}
System.out.println(" Occurrence(s) : " + ans);
}
}
Comments
Leave a comment