【问题标题】:trouble with boolean arrays and infinite loops布尔数组和无限循环的麻烦
【发布时间】:2012-11-02 12:29:09
【问题描述】:

我正在从事一个名为 life 的项目,该项目应该随机显示 1 表示活着或 0 表示死亡。当我执行程序时,零和一继续打印。我查看了代码,我找不到错误。

public class Life {
//Makes the first batch of cells
public static boolean firstgen(boolean[][] a)
{
    int N = 5;
    double cellmaker = Math.random();
    //boolean[][] b = new boolean[N][N];

    for (int i = 0; i < N; i++)
    {
        for (int j= 0; j< N;j++)
        {
            if (cellmaker >0.5)
            {
                a[i][j]= true;

                return true;
            }
            else
                a[i][j]=false;
        }
    }

    return false;

}


public static void main(String[] args)
{

 boolean[][] b = new boolean[5][5];



  //Placing the cells
  for (int i =0;i < 5; i++)
  {
      for (int j= 0 ; j < 5;i++)
      {
        if (firstgen(b)== true)
        {
            System.out.print("1"); //1 is live cell
        }
        else
            System.out.print("0");// 0 is dead cell 
     }

      System.out.println();
  }
}

}

【问题讨论】:

    标签: java arrays loops boolean


    【解决方案1】:

    在您的main 方法中

    for (int j= 0 ; j < 5;i++)
    

    您应该增加j 而不是i

    【讨论】:

    • 在main方法里,我复制粘贴了。
    【解决方案2】:

    您的随机调用在任何循环之外。因此,它是一个常数,它将使您始终处于循环状态。将随机调用放在循环中,你会没事的。

    public static boolean firstgen(boolean[][] a)
    {
        int N = 5;
        //boolean[][] b = new boolean[N][N];
    
        for (int i = 0; i < N; i++)
        {
            for (int j= 0; j< N;j++)
            {
                double cellmaker = Math.random();
                if (cellmaker >0.5)
                {
                    a[i][j]= true;
    
                    return true;
                }
                else
                    a[i][j]=false;
            }
        }
    
        return false;
    }
    

    另外,正如 Bhesh 所指出的,在这里将 i++ 更改为 j++

      for (int i =0;i < 5; i++)
      {
          for (int j= 0 ; j < 5;j++)
          {
            if (firstgen(b)== true)
            {
                System.out.print("1"); //1 is live cell
            }
            else
                System.out.print("0");// 0 is dead cell 
         }
    

    【讨论】:

    • 再次,Bhesh 指出的错误......它不存在。
    • @Intredasting public static void main(String[] args) 内的第二部分代码(向下滚动),if (firstgen(b)== true) 上面的行有该错误。 firstgenfor 循环没问题。
    • 道歉。由于 Windows 8 的新样式,我没有看到滚动条......从现在开始我必须小心。
    【解决方案3】:

    试试这些

    //Makes the first batch of cells
    public static boolean firstgen(boolean[][] a)
    {
        int N = 5;
        double cellmaker = Math.random();
        //boolean[][] b = new boolean[N][N];
    
        for (int i = 0; i < N; i++)
        {
            for (int j= 0; j< N;j++)
            {
                if (cellmaker >0.5)
                {
                    a[i][j]= true;
                    return true;
                }
                else
                    a[i][j]=false;
            }
        }
    
        return false;
    }
    
    
    public static void main(String[] args)
    {
        boolean[][] b = new boolean[5][5];
      //Placing the cells
        for (int i =0;i < 5; i++)
        {
            for (int j= 0 ; j < 5;j++)
            {
                if (firstgen(b))
                {
                    System.out.print("1"); //1 is live cell
                }
                else
                    System.out.print("0");// 0 is dead cell 
            }
            System.out.println();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-24
      相关资源
      最近更新 更多