简体   繁体   中英

trouble with boolean arrays and infinite loops

I working on a project called life, which is supposed to randomly display either a 1 for alive or 0 for dead. When I execute the program, the zeros and ones keep printing. I looked through the code and I couldn't find wrong.

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();
  }
}

}

In the following in your main method

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

you should increment j instead of i .

Your random call is outside of any loop. It is therefore a constant, which will keep you in the loop. Put the random call inside the loop, and you'll be fine.

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;
}

Plus as was pointed out by Bhesh, change the i++ to a j++ here

  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 
     }

Try These

//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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM