我试着用Cell对象的数组来实现一个方法来设置一个板子。然后,该方法随机地将一个新字符串"C10“放在”-“字符串上。我的class和main在下面
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();
}
}而不是随机地将C10放在棋盘上,我得到了这个输出;
|---|C10|C10|C10|C10|C10|C10|C10|C10|C10|
|C10|---|---|---|---|---|---|---|---|---|
|---|---|---|---|---|---|---|---|---|---|
|---|---|---|---|---|---|---|---|---|---|
|---|---|---|---|---|---|---|---|---|---|
|---|---|---|---|---|---|---|---|---|---|
|---|---|---|---|---|---|---|---|---|---|
|---|---|---|---|---|---|---|---|---|---|
|---|---|---|---|---|---|---|---|---|---|
|---|---|---|---|---|---|---|---|---|---|谁能告诉我我做错了什么?谢谢。
发布于 2013-03-16 06:55:41
我不确定你在for循环中的意图是什么:
if (board[RanNum] == board[k])但它会导致for循环为每个k生成随机数,直到生成k,然后设置该单元格。因此,对于k == 1,它将始终为k == 2设置单元1、单元2,为k == 3设置单元3,依此类推。
我猜你想做的事情更像是:
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--;
}编辑:
正如您可能从评论+其他答案中意识到的那样,上面的代码并不是特别容易阅读。像这样的东西应该更好:
int chutCount = 0;
while (chutCount < Chut)
{
int randomNum = ran.nextInt(board.length);
if (board[randomNum].addSpaces == 0) // uninitialized
{
board[randomNum] = new Cell(-10);
chutCount++;
}
}发布于 2013-03-16 06:55:26
我不确定您到底想要实现什么,但是如果您消除了内部setBoard循环中的if else,那么您应该会得到C10的随机放置。
更改此设置:
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--;
}要这样做:
for (int k = 1; k <= Chut; k++) {
int RanNum = ran.nextInt(board.length); // Randomly replace the
// toString
this.board[RanNum] = new Cell(-10);
}发布于 2013-03-16 06:58:28
我想你指的是!=。
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--;
}https://stackoverflow.com/questions/15443101
复制相似问题