我一直在努力工作几个小时!我觉得解决方案可能非常简单,但我无法看到它的生命。
我想知道当var1或var 2的用户输入超出范围时,如何使userInput()部分中的do-while循环实际停止。小于0或大于10.这是原始问题:
修改PlayLife以便用户可以输入一系列 row,col坐标在1到10之间,以使细胞存活。输入一个值 小于1或大于10将是用户完成的信号。
import java.util.Scanner;
public class PlayLife
{
public static void main(String[] args)
{int answer;
World myWorld = new World(10, 10);
System.out.println("Enter a row and column number between 1 and 10 to make a cell live:");
myWorld.userInput();
System.out.println(myWorld);
System.out.println();`
世界
import java.util.Scanner;
import java.util.Random;
public class World
{
private Cell[][] board; //a 2 dimensional array of Cells called board
private int rows;
private int cols;
int var1 = 0, var2 = 0 , test;
int row, col;
public World(int numOfRows, int numOfCols)
{
int row, col;
rows = numOfRows;
cols = numOfCols;
board = new Cell[rows][cols];
//the following nested loop creates the rows X cols array of "dead" Cells
for(row = 0; row < rows; row++)
for(col = 0; col < cols; col++)
board[row][col] = new Cell(false);
}
public boolean getStatus(int row, int col)
{
return board[row][col].getStatus();
}
public void setStatus(int row, int col, boolean status)
{
board[row][col].setStatus(status);
}
public void makeAlive(int row, int col)
{
board[row][col].setStatus(true);
}
public void makeDead(int row, int col)
{
board[row][col].setStatus(false);
}
public void userInput()
{
Scanner keyboard = new Scanner(System.in);
for(col = 0; col < cols; col++)
for(row = 0; row < rows; row++)
do
{
var1 = keyboard.nextInt();
var2 = keyboard.nextInt();
}
while(var1 < 0 || var1 >= 10 || var2 < 0 || var2 >= 10);
board[var1][var2].setStatus(true);
}
public void randomFill()
{
int row, col;
Random pick = new Random();
for(row = 0 ; row < rows; row++)
for(col = 0; col < cols; col++)
if(pick.nextInt(4)==0) //make 25% of the cells alive
board[row][col].setStatus(true);
}
public String toString()
{
String boardPic = ""; //start with an empty string
int row, col;
for(row = 0; row < rows; row++)
{
for(col = 0; col < cols; col++)
if(board[row][col].getStatus())
boardPic = boardPic + "X"; //if the cell is alive, add an X to it
else
boardPic = boardPic + "."; //otherwise adda dot
boardPic = boardPic + "\n"; //after finishing all columns in a row, add a newline character
}
return boardPic;
}
// counts all live cells in a 3 X 3 array then subtracts 1 if the center cell is alive
private int countNeighbors(int curRow, int curCol)
{
int row, col, neighbors = 0;
for(row = curRow - 1; row <= curRow + 1; row++)
for(col = curCol - 1; col <= curCol + 1; col++)
if(board[row][col].getStatus())
neighbors++;
//Is the center cell alive?
if(board[curRow][curCol].getStatus())
neighbors--;
return neighbors;
}
//The outside edge is already dead, so this only processes row 1-8 and column 1-8
//if a cell has three neighbors, it will be alive regardless of its current status
//if a cell has two neighbors, it will be the same in the next gen as the last.
public void nextGen()
{
World nextOne = new World(rows, cols); //makes a new "scratch" array
int row, col, neighbors;
for(row = 1; row < rows - 1; row++)
for(col = 1; col < cols - 1; col++)
{
neighbors = countNeighbors(row, col);
if(neighbors == 3)
nextOne.makeAlive(row, col);
else if (neighbors == 2)
nextOne.setStatus(row, col, this.getStatus(row, col)); //"this" refers to the object performing the method
}
//copy new world just created to exixting world
for(row = 0; row < rows; row++)
for(col = 0; col < cols; col++)
board[row][col].setStatus(nextOne.board[row][col].getStatus());
}
}
这是另一个。这个问题没有任何问题,我只是发布它,以防万一你想看到它
public class Cell
{
private boolean status; //true = alive, false = dead
public Cell(boolean aliveOrDead) //one parameter constructor
{
status = aliveOrDead;
}
public void setStatus(boolean aliveOrDead) //mutator to set the status of the cell via the parameter
{
status = aliveOrDead;
}
public boolean getStatus() //acessor to get the current status
{
return status;
}
}
答案 0 :(得分:0)
这样的事情
do
{
var1 = keyboard.nextInt();
var2 = keyboard.nextInt();
}
while((var1 > 0 && var1 < 10) && (var2 > 0 && var2 < 10));