私はデバッグに問題があるとの宿題があります。プログラムの目的は、行番号と列番号が同じで、大小の対角線があることを示すことです。これまでは、同じ行と列を見つけ出して印刷していました。ここで 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++;
}
}
}
ありがとうございました!もう1つ、ボードのメジャーとマイナーな対角線を見つける良い方法は何でしょうか? – Flinze