Add OOP/Java/Lab/Week4/SearchCount.java
This commit is contained in:
parent
3acb98a896
commit
46fa59b5f4
37
OOP/Java/Lab/Week4/SearchCount.java
Normal file
37
OOP/Java/Lab/Week4/SearchCount.java
Normal file
@ -0,0 +1,37 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
class SearchCount {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println(
|
||||
"Enter the number of rows and columns in the matrix: "
|
||||
);
|
||||
int m = sc.nextInt();
|
||||
int n = sc.nextInt();
|
||||
|
||||
int[][] matrix = new int[m][n];
|
||||
System.out.println("Enter the elements of the matrix:");
|
||||
int count = 0;
|
||||
System.out.println("Enter the element to search for:");
|
||||
int searchElement = sc.nextInt();
|
||||
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
int element = sc.nextInt();
|
||||
matrix[i][j] = element;
|
||||
if (element == searchElement) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(
|
||||
"The element " +
|
||||
searchElement +
|
||||
" occurs " +
|
||||
count +
|
||||
" times in the matrix."
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user