Scanner sc = new Scanner(System.in); int x = sc.nextInt(); sc.close(); int[][] matrix = new int[x][x]; int count = 1; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix.length; j++) { matrix[i][j] = count++; } } for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix.length; j++) { System.out.print(matrix[i][j] + "\t"); } System.out.println(); } 

There is a typical code for displaying the matrix on the screen, but at the exit the top lines move out. What is the reason?

Conclusion:

  • MB because the top two lines mainly consist of single digits, and the rest - from two-digit? - br3t
  • Yes, it is, problems with the conclusion is solved if the two-digit numbers at the top, but how to make it universal? - FlowMyStack

2 answers 2

Problem with output in your particular console. Try another, everything should work.

enter image description here

  • strange. On a Mac, it also displays normally, but does not output it on two Windows. - FlowMyStack
 Scanner sc = new Scanner(System.in); int x = sc.nextInt(); sc.close(); int[][] matrix = new int[x][x]; int count = 1; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix.length; j++) { matrix[i][j] = count++; } } for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix.length; j++) { System.out.print((matrix[i][j] > 9 ? "" : " ") + matrix[i][j] + "\t"); } System.out.println(); } 
  • You have an empty literal after a coaching operator. At the stage of compilation will still die - FlowMyStack