-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch2DArray.java
More file actions
60 lines (49 loc) · 1.63 KB
/
Search2DArray.java
File metadata and controls
60 lines (49 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.Scanner;
class Search2DArray{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Welsome to Find Num in 2D Array Method");
int[][] numArray = inputArray2D();
System.out.print("Enter the number which is you want to search: ");
int num = input.nextInt();
boolean result = NumSearch(numArray, num);
if (result) {
System.out.print("Your Number is Found in 2D Array");
}else{
System.out.print("Your Number is Not Found in 2D Array");
}
}
public static int[][] inputArray2D(){
Scanner input = new Scanner(System.in);
System.out.print("Please Enter Number of Rows: ");
int rows = input.nextInt();
System.out.print("Please Enter Number of Column: ");
int column = input.nextInt();
int[][] numArray = new int[rows][column];
int i = 0;
while (i < rows) {
int j = 0;
while (j < column) {
System.out.print("Please Enter element row: " + (i+1) + ", column " + (j+1) + " : ");
numArray[i][j] = input.nextInt();
j++;
}
i++;
}
return numArray;
}
public static boolean NumSearch(int[][] Array, int UserNum){
int i = 0;
while (i < Array.length) {
int j = 0;
while (j <Array[i].length) {
if (Array[i][j] == UserNum) {
return true;
}
j++;
}
i++;
}
return false;
}
}