Printing an invalid method twice

Hello, I am trying to understand the code that I wrote, and why it outputs the result below.

public void isSymmetricNow(int[][] matrix){ for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix.length; j++) { if (matrix[i][j] != matrix[j][i]) { System.out.print("matrix is not symmetric \n"); } } } System.out.print("matrix is symmetric \n"); } 

prints me

 matrix is not symmetric matrix is not symmetric matrix is symmetric 

ACCEPT that this matrix is ​​not symmetrical here.

 int matrix3[][] = {{1,4,7},{-4,6,6},{7,6,9}}; 

How can I change this code to return an answer if the matrix is ​​symmetric or not just once.

+6
source share
4 answers

Only a simple return statement. It will not be executed again if the condition is false.

 public void isSymmetricNow(int[][] matrix){ //Random random = new Random(); for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix.length; j++) { //matrix[i][j] = random.nextInt(20); if (matrix[i][j] != matrix[j][i]) { System.out.print("matrix is not symmetric \n"); return; } } } System.out.print("matrix is symmetric \n"); } 

Or

You can return a boolean expression that is symmetric or not.

 public boolean isSymmetricNow(int[][] matrix){ //Random random = new Random(); for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix.length; j++) { //matrix[i][j] = random.nextInt(20); if (matrix[i][j] != matrix[j][i]) { return false; } } } return true; } 

then call it using your function.

 if(isSymmetric(matrix)) System.out.println("Symmetric"); else System.out.println("Not Symmetric"); 
+6
source

First of all, your loops will print "the matrix is ​​asymmetric \ n" whenever they find i and j for which matrix[i][j] != matrix[j][i] , which can happen more than once.

and System.out.print("matrix is symmetric \n"); always called, therefore explains the last line of output.

You probably want your method to have a boolean return value instead of printing this output. Thus, loops will only be repeated until you know that the matrix is ​​not symmetrical.

 public boolean isSymmetricNow(int[][] matrix){ //Random random = new Random(); for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix.length; j++) { //matrix[i][j] = random.nextInt(20); if (matrix[i][j] != matrix[j][i]) { return false; } } } return true; } 

To do the same without returning value:

 public void isSymmetricNow(int[][] matrix){ //Random random = new Random(); boolean isSymmetric = true; for (int i = 0; i < matrix.length && isSymmetric; i++) { for (int j = 0; j < matrix.length && isSymmetric; j++) { //matrix[i][j] = random.nextInt(20); if (matrix[i][j] != matrix[j][i]) { System.out.print("matrix is not symmetric \n"); isSymmetric = false; } } } if (isSymmetric) System.out.print("matrix is symmetric \n"); } 
+4
source

1) As Eran noted, I and j are not equal more than once and therefore will print as long as the cycle repeats. I suggest using the break statement to exit the loop the first time it finds that I! = J.

2) The last print statement will be called regardless of how the cycle behaves and whether the matrix is ​​symmetric or not. I suggest printing that the matrix is ​​symmetrical, creating another if statement outside the loop to check for symmetry.

0
source
 public void isSymmetricNow(int[][] matrix){ //Random random = new Random(); for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix.length; j++) { //matrix[i][j] = random.nextInt(20); if (matrix[i][j] != matrix[j][i]) { System.out.print("matrix is not symmetric \n"); return; //When it does not symetric, return. } } } System.out.print("matrix is symmetric \n"); } 
-1
source

All Articles