Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 检查值方法不适用于检查幻方_Java - Fatal编程技术网

Java 检查值方法不适用于检查幻方

Java 检查值方法不适用于检查幻方,java,Java,即使输入了正确的数字,此方法也始终返回false。如果输入了数字1-n^2且没有重复,则应返回true public boolean checkValues() { int numCounter=1; boolean okay=false; while (numCounter<=n*n) { for (int i=0; i< n ; i++) { for (int j=0; j< n ; j++) {

即使输入了正确的数字,此方法也始终返回false。如果输入了数字1-n^2且没有重复,则应返回true

   public boolean checkValues()
{
  int numCounter=1;
  boolean okay=false;
  while (numCounter<=n*n)
  {
     for (int i=0; i< n ; i++) 
     {
        for (int j=0; j< n ; j++)
        {
           if ((int) square.get(i).get(j)==numCounter)
              okay=true;  
        }
     }
     okay=false;
     numCounter++;
  }
  if (numCounter==n*n)
     return true;
  else 
     return false;  
}
public boolean checkValues()
{
int numCounter=1;
布尔值OK=false;

while(numCounter将
if(numCounter==n*n)
更改为
if(numCounter==n*n+1)
,因为while循环最终将再次执行
numCounter++;
或替换
while(NumCounter请进一步解释此方法所需的值。你不应该期望每个人都知道“magi square”。当代码无法正常工作时,很难找到你特别想要实现的内容。抱歉!我正在做的是将数字输入arraylist的arraylist中,并检查数字是否为1-n^2(n^2是较大的arraylist的大小)是否存在,并且没有重复项。幻方还有更多内容,但我有这些方法可以使用。唯一的问题是,将OK添加回中会使它卡在一个无限空间中loop@newAtJava我刚刚在while循环的末尾添加了numCounter++,这是必需的
public boolean checkValues()
{
  int numCounter=1;
  boolean okay=false;
  while (numCounter<=n*n)
  {
     ok = false;
     for (int i=0; i< n ; i++) 
     {
        for (int j=0; j< n ; j++)
        {
           if ((int) square.get(i).get(j)==numCounter)
              ok=true;  
        }
     }
     if(!ok)  // numCounter cannot be found
         return false;
     numCounter++;
  }
  return true; // successfully passed the check through 1 to n^2 
}
public boolean checkValues()
{
     Set<Integer> total = new HashSet<Integer>();
     for (int i=0; i< n ; i++){
        for (int j=0; j< n ; j++){
           int num = square.get(i).get(j);
           if(num>=1 && num<=n*n)
               total.add(num);
        }
     }
     if(total.size() == n*n)
        return true;
     else
        return false;
}