2016-04-04 130 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