diff --git a/HourGlass.class b/HourGlass.class new file mode 100644 index 0000000..401a3e9 Binary files /dev/null and b/HourGlass.class differ diff --git a/HourGlass.java b/HourGlass.java index cdf5ddc..d67dc15 100644 --- a/HourGlass.java +++ b/HourGlass.java @@ -1,3 +1,5 @@ +import java.util.Scanner; + public class HourGlass { /* @@ -10,12 +12,34 @@ public class HourGlass { */ public static void main(String[] args) { - - int[][] ar = { { 1, 1, 1, 0, 0, 0 }, { 0, 1, 0, 0, 0, 0 }, { 1, 1, 1, 0, 0, 0 }, { 0, 0, 2, 4, 4, 0 }, - { 0, 0, 0, 2, 0, 0 }, { 0, 0, 1, 2, 4, 0 } }; + int[][] ar = matrix(); System.out.println(sumInt(ar)); } + private static int[][] matrix() { + Scanner scanner = new Scanner(System.in); + System.out.println("Please enter the 2D Matrix elements below:"); + System.out.println("Numbers of rows: "); + int rows = scanner.nextInt(); + System.out.println("Numbers of Columns: "); + int columns = scanner.nextInt(); + int[][] ar = new int[rows][columns]; + for (int i = 0; i < rows; i++) { + for (int j = 0; j < columns; j++) { + ar[i][j] = scanner.nextInt(); + } + System.out.println(); + } + for (int i = 0; i < rows; i++) { + for (int j = 0; j < columns; j++) { + System.out.print(ar[i][j] + "\t"); + } + System.out.println(); + } + scanner.close(); + return ar; + } + private static int sumInt(int[][] arr) { int rows = arr.length; int columns = arr[0].length;