0

我现在正在用 Java 开发一个数独解决方案检查器,它被分成两部分。第二部分希望在代码中添加五个新的“特定方法”,它们是对行、列、块的布尔检查,然后在循环中返回它们是否为真或假。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()
  {
  }

我认为现在让我感到困惑的主要部分是这与我已经创建的检查这些东西的代码有什么不同......所以我对我被问到什么感到困惑。

4

1 回答 1

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;
 }
于 2013-12-03T06:54:51.063 回答