2016-04-04 8 views
1

私はデバッグに問題があるとの宿題があります。プログラムの目的は、行番号と列番号が同じで、大小の対角線があることを示すことです。これまでは、同じ行と列を見つけ出して印刷していました。ここで Java - 2Dアレイボードカウンタ

は、これまでのプログラムの出力です:

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

であると信じています。私はcheckColのための方法のためにちょうど2つでそれをいかにすることができるか説明します。あなたはまだループのために、この内側に続いて0からarray[i].length - 1

に行く0からarray.length - 1

jに行くループ

iのためのあなたの外側の2を持っているでしょう

、あなたはarray[i][j]であれば、すべての要素をチェックarray[0][j](これは特定の列の0行目の要素です)に等しくなります。

特定の列の要素のいずれかが列の0行目の要素と等しくない場合にfalseに設定するブール値フラグを維持します。 forループを入力する前に、このフラグがtrueに設定されていることを確認してください。

内側のforループの外側では、フラグがtrueに設定されているかどうかをチェックし、その特定の列と数値を印刷するかどうかをチェックします。フラグをtrueにリセットします。

これはおそらく、列を複数回印刷する問題を解決すると思います。行についても同様のことができます。

手順がわからない場合は教えてください。

+0

ありがとうございました!もう1つ、ボードのメジャーとマイナーな対角線を見つける良い方法は何でしょうか? – Flinze

1

は、私はあなたがあなたの3 checkRowcheckCol方法でループのためにネストされて必要とは思わない、これはあなたの問題

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

私は両方をやってみました。続けてください。 do not work – Flinze

1

この問題は、@maesydyで示唆されているように、2つの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; 'if(a [r] [c]!= a [r] [c + 1])' '{ isMatching = false; 休憩。 } else { num = a [r] [c]; } ' //一致すると出力にnumを出力します – MSameer