Advertisement
nov0

Z9_PrintMatrix

May 30th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.82 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Z9_MatrixN {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner input = new Scanner(System.in);
  7.  
  8.         System.out.println("Enter dimension of matrix: ");
  9.         printMatrix(input.nextInt());
  10.  
  11.         input.close();
  12.     }
  13.  
  14.     public static void printMatrix(int n) {
  15.         int[][] matrix = new int[n][n];
  16.         for (int i = 0; i < n; i++) {
  17.             for (int j = 0; j < n; j++) {
  18.                 matrix[i][j] = (int) (Math.random() * 2);
  19.             }
  20.         }
  21.  
  22.         for (int[] matr : matrix) {
  23.             for (int i : matr) {
  24.                 System.out.print(i + " ");
  25.             }
  26.             System.out.println();
  27.         }
  28.     }
  29.    
  30.     /* Jednostavnije rjesenje
  31.     public static void printMatrix(int n) {
  32.         for (int i = 0; i < n; i++) {
  33.             for (int j = 0; j < n; j++) {
  34.                 System.out.print((int) (Math.random() * 2) + " ");
  35.             }
  36.             System.out.println();
  37.         }
  38.     }
  39.     */
  40.  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement