简体   繁体   English

随机生成器不起作用

[英]Random generator does not work

i tried to implement a method to set up a board with an array of Cell object. 我试图实现一种用Cell对象数组设置板的方法。 the method then randomly place a new string "C10" over the "---" string. 然后,该方法会在“ ---”字符串上随机放置一个新字符串“ C10”。 My class and main is below 我的班级和主要在下面

public class Cell {
    public int addSpaces;

    public Cell() {
        addSpaces = 0;
    }

    public Cell(int x) {
        addSpaces = x;
    }

    public String toString() {
        String print;
        if (addSpaces == -10)
            print = "C10";
        else
            print = "---";
        return print;
    }
}

import java.util.Random;
public class ChutesAndLadders {
    Cell[] board = new Cell[100]; // Set array of Cell object
    Random ran = new Random();
    Cell s = new Cell();
    public int Chut, Ladd;

    public ChutesAndLadders() {
    }

    public ChutesAndLadders(int numChutes, int numLadders) {
        Chut = numChutes;
        Ladd = numLadders;
    }

    public void setBoard() {
        for (int i = 0; i < board.length; i++)
            board[i] = new Cell(); // board now has 100 Cell with toString "---"
        for (int k = 1; k <= Chut; k++) {
            int RanNum = ran.nextInt(board.length); // Randomly replace the
                                                    // toString
            if (board[RanNum] == board[k])
                this.board[RanNum] = new Cell(-10);
            else
                k--;
        }
    }

    public void printBoard() { // method to print out board
        int count = 0;
        for (int i = 0; i < board.length; i++) {
            count++;
            System.out.print("|" + board[i]);
            if (count == 10) {
                System.out.print("|");
                System.out.println();
                count = 0;
            }
        }
    }

    public static void main(String[] args) {
        ChutesAndLadders cl = new ChutesAndLadders(10, 10);
        cl.setBoard();
        cl.printBoard();
    }
}

Instead of randomly placing C10 all over the board I got this output; 我没有将C10随机放置在板上,而是得到了此输出;

|---|C10|C10|C10|C10|C10|C10|C10|C10|C10|
|C10|---|---|---|---|---|---|---|---|---|
|---|---|---|---|---|---|---|---|---|---|
|---|---|---|---|---|---|---|---|---|---|
|---|---|---|---|---|---|---|---|---|---|
|---|---|---|---|---|---|---|---|---|---|
|---|---|---|---|---|---|---|---|---|---|
|---|---|---|---|---|---|---|---|---|---|
|---|---|---|---|---|---|---|---|---|---|
|---|---|---|---|---|---|---|---|---|---|

can someone tell me what i did wrong? 有人可以告诉我我做错了什么吗? Thank you. 谢谢。

I'm not sure what your intention with this was in your for-loop: 我不确定您对此的意图是在您的for循环中:

if (board[RanNum] == board[k])

But it will cause your for-loop to, for each k, generate random numbers until it generates k, and then set that cell. 但这会使您的for循环为每个k生成随机数,直到生成k,然后设置该单元格。 So for k == 1, it will always set cell 1, cell 2 for k == 2, cell 3 for k == 3, etc. 因此,对于k == 1,它将始终将单元格1设置为k == 2,将单元格3设置为k == 3,以此类推。

I'm guessing you want to do something more like: 我猜你想做更多的事情:

for (int k = 1; k <= Chut; k++) {
    int RanNum = ran.nextInt(board.length);
    if (board[RanNum].addSpaces == 0) // uninitialized
        this.board[RanNum] = new Cell(-10);
    else
        k--;
}

EDIT: 编辑:

As you probably realized from the comment + other answers, the above code is not particularly readable. 正如您可能从注释和其他答案中意识到的那样,上面的代码不是特别可读。 Something like this should be better: 这样的事情应该更好:

int chutCount = 0;
while (chutCount < Chut)
{
    int randomNum = ran.nextInt(board.length);
    if (board[randomNum].addSpaces == 0) // uninitialized
    {
        board[randomNum] = new Cell(-10);
        chutCount++;
    }
}

I'm not sure exactly what you want to achieve, but if you eliminate the if else in your inner setBoard loop then you should get random placement of C10 . 我不确定您到底想实现什么,但是如果您在内部setBoard循环中消除了if elseif else ,那么您应该随机获得C10

Change this: 更改此:

    for (int k = 1; k <= Chut; k++) {
        int RanNum = ran.nextInt(board.length); // Randomly replace the
                                                // toString
        if (board[RanNum] == board[k])
            this.board[RanNum] = new Cell(-10);
        else
            k--;
    }

To this: 对此:

    for (int k = 1; k <= Chut; k++) {
        int RanNum = ran.nextInt(board.length); // Randomly replace the
                                                // toString
        this.board[RanNum] = new Cell(-10);
    }

I think you meant != . 我认为您的意思是!=

for (int k = 1; k <= Chut; k++) {
   int ranNum = (int)(Math.random()*board.length);
   if (board[ranNum] != board[k])
      this.board[ranNum] = new Cell(-10);
   else
      k--;
}

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

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