binibiningtinamoran

Matrix.java

Nov 23rd, 2019
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Matrix {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         Scanner scan = new Scanner(System.in);
  8.  
  9.         System.out.println("Welcome to the Matrix Maker");
  10.         System.out.println("=============================");
  11.         System.out.println("\nHere are your options:");
  12.         System.out.println("1 \t To enter matrix data" +
  13.                 "\n2 \t To display matrix" +
  14.                 "\n3 \t To exit\n");
  15.  
  16.         System.out.print("Enter your option here: ");
  17.         int choiceMade = Integer.parseInt(scan.nextLine());
  18.         int[][] yourMatrix = new int[0][0];
  19.        
  20.         switch(choiceMade) {
  21.             case 1:
  22.                 int matrixRow = getRows(scan);
  23.                 int matrixCol = getCols(scan);
  24.                 yourMatrix = new int[matrixRow][matrixCol];
  25.                 enterMatrixData(scan, yourMatrix);
  26.             case 2:
  27.                 printMatrix(yourMatrix);
  28.                 break;
  29.             case 3:
  30.                 System.out.println("Goodbye!");
  31.                 System.exit(0);
  32.             default:
  33.                 System.out.println("Invalid choice!");
  34.         }
  35.     }
  36.  
  37.     public static int getRows(Scanner s) {
  38.         System.out.print("Enter the number of matrix rows: ");
  39.         int row = Integer.parseInt(s.nextLine());
  40.  
  41.         while (row <= 0) {
  42.             System.out.print("Enter the number Of matrix rows: ");
  43.             row = Integer.parseInt(s.nextLine());
  44.         }
  45.         return row;
  46.     }
  47.  
  48.     public static int getCols(Scanner s) {
  49.         System.out.print("Enter the number of matrix columns: ");
  50.         int cols = s.nextInt();
  51.  
  52.         while (cols <= 0) {
  53.             System.out.print("Enter the number Of matrix rows: ");
  54.             cols = Integer.parseInt(s.nextLine());
  55.         }
  56.         return cols;
  57.     }
  58.  
  59.     public static void enterMatrixData(Scanner s, int[][] m) {
  60.         for (int i = 0; i < m.length; i++) {
  61.             for (int j = 0; j < m[i].length; j++) {
  62.                 System.out.printf("Enter value for Row %,d, Column %,d: ", i+1, j+1);
  63.                 m[i][j] = s.nextInt();
  64.             }
  65.         }
  66.     }
  67.  
  68.     public static void printMatrix(int[][] matrix) {
  69.  
  70.         if (matrix.length == 0) {
  71.             System.out.println("Matrix is empty!");
  72.         } else {
  73.             for (int[] rowVals : matrix) {
  74.                 for (int colVals : rowVals) {
  75.                     System.out.print(colVals + "\t");
  76.                 }
  77.                 System.out.println();
  78.             }
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment