2016-04-04 54 views
1

我有一个家庭作业问题,我有麻烦调试。程序的目的是说明哪些行和列具有相同的数字,以及主要和次要对角线。到目前为止,我已经找到了相同的行和列,然后将它们打印出来。Java - 二维阵列板计数器

这里是程序的输出迄今:

0 0 0 0 0 0 0 0 

0 0 1 0 1 0 0 0 

0 0 0 0 1 0 1 0 

0 0 1 0 0 1 1 0 

0 0 1 0 0 1 1 0 

0 0 0 0 0 0 1 0 

0 0 0 0 0 0 0 0 

0 0 1 1 1 1 1 0 

All 0 on row 0 

All 0 on column 0 

All 0 on column 1 

All 0 on column 1 

All 0 on column 7 

All 0 on column 7 

正如你可以看到列打印重复了,我想不通为什么和如何解决它。我也有一个问题,它不会显示行6,因为它们都是一样的。

我的期望输出应该是:

All 0 on row 0 

All 0 on row 6 

All 0 on column 0 

All 0 on column 1 

All 0 on column 7 

预先感谢您。

import java.util.Scanner; 

public class javaTest 
{ 
// Main method 
public static void main(String[] args) 
{ 

    int[][] array = { 
     {0,0,0,0,0,0,0,0}, 
     {0,0,1,0,1,0,0,0}, 
     {0,0,0,0,1,0,1,0}, 
     {0,0,1,0,0,1,1,0}, 
     {0,0,1,0,0,1,1,0}, 
     {0,0,0,0,0,0,1,0}, 
     {0,0,0,0,0,0,0,0}, 
     {0,0,1,1,1,1,1,0} 
    }; 

    for(int i = 0; i < array.length; i++) 
    { 
     for(int j = 0; j < array[i].length; j++) 
      System.out.print(array[i][j] + " "); 

     System.out.println(); 
    } 
    checkRow(array); 
    checkCol(array); 
} 

// Check if the row is the same 
public static void checkRow(int array[][]) 
{ 
    String checkRow = ""; 
    int rowCount = 0; 
    int count = 0; 
    for(int i = 0; i < array.length; i++) 
    { 
     for(int j = 0; j < array[i].length;j++) 
     { 
      // Create a new array to compare 
      int num = array[i][j]; 
      for(int k = 0; k < array[i].length; k++) 
      { 
       // Check if the first number of the row is equal to the next 
       if(num == array[j][k]) 
        // If so increment count 
        count++; 
       else 
        count = 0; 
      } 
      // If all numbers of the row is the same, total would be 8 and print 
      if(count == array.length) 
       System.out.println("All " + num + " on row " + rowCount); 
     } 
     rowCount++; 
    } 
} 

// Check if column is the same 
public static void checkCol(int array[][]) 
{ 
    String checkCol = ""; 
    int colCount = 0; 
    int count = 0; 
    for(int i = 0; i < array.length; i++) 
    { 
     for(int j = 0; j < array[i].length; j++) 
     { 
      int num = array[i][j]; 
      for(int k = 0; k < array[i].length; k++) 
      { 
       if(num == array[k][i]) 
        count++; 
       else 
        count = 0; 
      } 
      if(count == array.length) 
       System.out.println("All " + num + " on column " + colCount); 
     } 
     colCount++; 
    } 
} 

}

回答

3

我不认为你需要嵌套for循环在你checkRowcheckCol方法3。我将解释如何用checkCol的方法仅用2。

你仍然有你的for循环

i会从0到array.length - 1

j从0到array[i].length - 1

然后这里面for循环,你array[i][j]检查每一个元素外2等于array[0][j](这是该特定列的第0行的元素)。

如果特定列中的任何元素不等于该列第0行的元素,则维护一个布尔标志,该布尔标志设置为false。确保在甚至输入for循环之前将该标志设置为true。

在inner for循环之外,您检查标志是否设置为true,如果是这样,则打印该特定列和数字。将标志重置为true。

我认为这可能会解决打印列多次的问题。你也可以为行做类似的事情。

如果您不了解任何步骤,请告诉我。

+0

谢谢!还有一件事,找到董事会主要和次要对角线的好方法是什么? – Flinze

1

我认为,这是你的问题

if(count == array.length) 
      System.out.println("All " + num + " on row " + rowCount); 
    } 
    rowCount++;// this is inside the first for loop but not the second. so it has to go through all the other inner for loops before it can count this again so its skipping rows 
+0

我已经尝试过这两个,似乎要么休息;并继续;不工作 – Flinze

1

这个问题可以用@maesydy建议的两个for-loops来解决。这里是输出的实现。

public class Test { 
// Main method 
public static void main(String[] args) { 

    int[][] array = { 
      {0, 0, 0, 0, 0, 0, 0, 0}, 
      {0, 0, 1, 0, 1, 0, 0, 0}, 
      {0, 0, 0, 0, 1, 0, 1, 0}, 
      {0, 0, 1, 0, 0, 1, 1, 0}, 
      {0, 0, 1, 0, 0, 1, 1, 0}, 
      {0, 0, 0, 0, 0, 0, 1, 0}, 
      {0, 0, 0, 0, 0, 0, 0, 0}, 
      {0, 0, 1, 1, 1, 1, 1, 0} 
    }; 

    for (int i = 0; i < array.length; i++) { 
     for (int j = 0; j < array[i].length; j++) 
      System.out.print(array[i][j] + " "); 

     System.out.println(); 
    } 
    checkRow(array); 
    checkCol(array); 
} 

/** 
* Check if all elements of row are same 
* @param a array 
*/ 
public static void checkRow(int a[][]) { 
    for (int r= 0; r < a.length; r++) { 
     int rowNum = r + 1; 
     boolean isMatching = true; 
     for (int c = 0; c < a[r].length -1; c++) { 
      //Compare two subsequent columns in same column 
       if(a[r][c] != a[r][c+1]) { 
        isMatching = false; 
        break; 
       } 
      } 
     //If all elements matched print output 
     if(isMatching) { 
      System.out.println("Row " + rowNum + " has all matching elements"); 
     } 
    } 
} 

/** 
* Check if all elements of column are same 
* @param a array 
*/ 
public static void checkCol(int a[][]) { 
    for (int c = 0; c < a.length; c++) { 
     int colNum = c + 1; 
     boolean isMatching = true; 
     for (int r = 0; r < a[c].length -1; r++) { 
      //Compare two subsequent rows in same column 
      if(a[r][c] != a[r+1][c]) { 
       isMatching = false; 
       break; 
      } 
     } 
     //If all elements matched print output 
     if(isMatching) { 
      System.out.println("Column " + colNum + " has all matching elements"); 
     } 
    } 
} 

}

注:我已经使用名为R/C描绘的行和列索引的索引。

输出:

0 0 0 0 0 0 0 0 
0 0 1 0 1 0 0 0 
0 0 0 0 1 0 1 0 
0 0 1 0 0 1 1 0 
0 0 1 0 0 1 1 0 
0 0 0 0 0 0 1 0 
0 0 0 0 0 0 0 0 
0 0 1 1 1 1 1 0 
Row 1 has all matching elements 
Row 7 has all matching elements 
Column 1 has all matching elements 
Column 2 has all matching elements 
Column 8 has all matching elements 

希望这有助于。开心编程,尽情享受!

+0

谢谢!虽然我将如何显示匹配的数字?例如第1行的所有匹配元素的编号为0.或第1列的所有匹配元素的编号为0. – Flinze

+0

如果要捕获实际的编号,可以保留一个临时变量,如下所示: int num;如果(a [r] [c]!= a [r] [c + 1])' '{ isMatching = false; 休息; } else { num = a [r] [c]; }' //在匹配时在您的输出中打印num – MSameer