[英]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.