简体   繁体   English

当输入超出范围时,如何使do-while循环停止?

[英]How to make this do-while loop stop when the input is out of range?

I've been working on this for hours! 我已经为此工作了几个小时! I feel like the solution might be really easy but I just can't see it out for the life of me. 我觉得解决方案可能真的很简单,但我一生都看不出来。

I want to know how to make the do-while loop in the userInput() part actually stop when the user input for var1 or var 2 is outside of the range. 我想知道当var1或var 2的用户输入超出范围时,如何使userInput()部分中的do-while循环实际停止。 Less than 0 or greater than 10. Here's the original question: 小于0或大于10。这是原始问题:

modify PlayLife so the user can enter a series of row, col coordinates between 1 and 10 to make cells alive. 修改PlayLife,以便用户可以输入一系列的行,在1到10之间的col坐标以使细胞存活。 Entering a value less than 1 or greater than 10 will be the signal the user is done. 输入小于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();`

World 世界

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

and here's the other one. 这是另一个。 There's nothing wrong with this one I'm just posting it just in case you'd like to see it 这个没有什么错,我只是将其发布,以防万一您希望看到它

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

Seth is right. 塞思是对的。 The expression you use inside your while is not correct for what you are trying to achieve. 您在while内使用的表达方式与您要实现的目的不正确。 If you replace it with the code Seth gave you you should be fine. 如果将其替换为Seth给您的代码,则应该没问题。 Right now your code executes only for the values for which you DONT want it to execute. 现在,您的代码仅对您不希望执行的值执行。

So something like this 所以像这样

do
            {
                var1 = keyboard.nextInt();
                var2 = keyboard.nextInt();
            }
            while((var1 > 0 && var1 < 10) && (var2 > 0 && var2 < 10));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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