[英]How to make this do-while loop stop when the input is out of range?
我已经为此工作了几个小时! 我觉得解决方案可能真的很简单,但我一生都看不出来。
我想知道当var1或var 2的用户输入超出范围时,如何使userInput()部分中的do-while循环实际停止。 小于0或大于10。这是原始问题:
修改PlayLife,以便用户可以输入一系列的行,在1到10之间的col坐标以使细胞存活。 输入小于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;
}
}
塞思是对的。 您在while内使用的表达方式与您要实现的目的不正确。 如果将其替换为Seth给您的代码,则应该没问题。 现在,您的代码仅对您不希望执行的值执行。
所以像这样
do
{
var1 = keyboard.nextInt();
var2 = keyboard.nextInt();
}
while((var1 > 0 && var1 < 10) && (var2 > 0 && var2 < 10));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.