subject

Language: Java I'd like for my code to print "O" 30% of the time, but it prints "O" 100% of the time. How can I fix that?

Code:

public class PetriDish {
private boolean[][] grid;

public PetriDish()
{
// Formula: (int)(Math. random() * (max - min + 1) + min);
int row = (int)(Math. random() * (30 - 5 + 1) + 5);
int column = (int)(Math. random() * (30 - 5 + 1) + 5);
boolean[][] grid2 = new boolean [row][column];
for(int i = 0; i < row; i++)
{
for(int j = 0; j < column; j++)
{

grid2[i][j] = Math. random() < 0.30;
}
}
grid = grid2;
}

public void showGrid()
{
for(int row = 0; row < grid. length; row++)
{
for(int column = 0; column < grid[0].length; column++)
{
if (grid[row][column] = true)
System. out. print("O"); //true state
else
System. out. print("."); //False State
}
System. out. println();
}

}

public int numNeighbors(int row, int col)
{
int count = 0;
for(int i = row - 1; i <= row + 1 ; i++)
{
for(int j = col - 1; j <= col + 1; j++)
{
if ((i >= 0) && (i < grid. length))
{
if ((j >= 0) && (j < grid. length))
{
if ((i != row ) && (j != col))
{
if (grid[i][j] = true)
count++;

}
}
}
}
}
return count;
}

public static void main(String args[])
{
PetriDish p = new PetriDish();
p. showGrid();
/*Change the (4,4) to be a random valid value within the grid*/
System. out. println(p. numNeighbors(0,0));
}
}

ansver
Answers: 1

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 20:00
What statement best describes operating systems? it’s possible for modern computers to function without operating systems. most operating systems are free or very inexpensive. operating systems are managed by the computer’s microprocessor (cpu). operating systems manage the computer’s random access memory (ram).
Answers: 1
question
Computers and Technology, 23.06.2019 20:30
What is the biggest difference between section breaks and regular page breaks
Answers: 1
question
Computers and Technology, 24.06.2019 04:30
Fall protection, confined space entry procedures, controlled noise levels, and protection from chemical hazards are some of the things that contribute to a safe what
Answers: 1
question
Computers and Technology, 24.06.2019 10:40
Joe needs to see the slide transitions and animations he has applied to his slides in a large view. which presentation view should he use? in which tab would joe find the animations option to make further changes, if any?
Answers: 1
You know the right answer?
Language: Java I'd like for my code to print "O" 30% of the time, but it prints "O" 100% of the tim...
Questions
question
Mathematics, 10.03.2021 01:40
question
Mathematics, 10.03.2021 01:40
question
Social Studies, 10.03.2021 01:40
question
Mathematics, 10.03.2021 01:40
Questions on the website: 13722367