我现在正在研究Java中的Sudoku Solution checker,它分为两部分。第二部分想要在代码中添加五个新的“特定方法”,它们是行,列,块的布尔检查,然后在循环中返回它们是真还是假。 checkAndPrintReport也被用来为每行的一次失败检查打印一行,检查列失败, 没能检查一个块。这就是我到目前为止所做的。
public boolean checkAndPrintReport( )
{
return false;
}
public boolean isGoodRow( int yRowParam )
{
int sum = 0;
for ( int x = 0; x <9; x++)
{
sum = sum + getCell (x,yRowParam);
}
return( true );
}
public boolean isGoodColumn( int xColParam )
{
int sum = 0;
for (int y = 0; y < 9 ;y++)
{
sum = sum + getCell (xColParam, y);
}
return( true );
}
public boolean isGoodBlock(int xBlockP, int yBlockP)
{
int sum = 0;
for (int x=0; x<3; x++)
{
for (int y=0; y<3;y++)
{
sum = sum + getCell (xBlockP+x, yBlockP+y);
}
}
return( true );
}
public boolean checkAll()
{
}
我认为现在令我困惑的主要部分是,这与我已经创建的检查这些内容的代码有何不同......所以我对被问到的问题感到困惑。
答案 0 :(得分:0)
定义
/** sum of 1+2+...+9 */
static final int SUM = 45;
然后你的代码看起来像这样
public boolean isGoodColumn( int xColParam ) {
int sum = 0;
for (int y = 0; y < 9 ;y++) {
sum = sum + getCell (xColParam, y);
}
return SUM == sum;
}
public boolean isGoodColumn( int xColParam ) {
int sum = 0;
for (int y = 0; y < 9 ;y++) {
sum = sum + getCell(xColParam, y);
}
return SUM == sum;
}
public boolean isGoodBlock(int xBlockP, int yBlockP) {
int sum = 0;
for (int x=0; x<3; x++) {
for (int y=0; y<3;y++) {
sum = sum + getCell(xBlockP+x, yBlockP+y);
}
}
return SUM == sum;
}