package penny_pinch_v2;
public class Board {
public static String[] board = new String[25];
public static void fillBoard() {
for (int i = 0; i < 25; i++) {
board[i] = "[ ]";
}
}
public static void printBoard() {
for (int i = 0; i < 25; i++) {
if (i == 5 || i == 10 || i == 15 || i == 20)
System.out.println();
System.out.format("%12s", board[i]);
}
}
}
package penny_pinch_v2;
import java.util.Random;
public class Pennies {
private static Random random;
public static void throwPennies() {
int count, randInt;
random = new Random();
for (int i = 0; i < 10; i++) {
randInt = random.nextInt(25);
count = 0;
while (count == 0) {
if (Board.board[randInt].charAt(1) != '[') {
Board.board[randInt] = "[" + Board.board[randInt] + "]";
count++;
}
}
}
}
}
package penny_pinch_v2;
import java.util.Random;
public class Prizes {
private static Random random;
public static String[] prizes = {
"Puzzle", "Poster", "Ball", "Game", "Doll"
};
public static int[] prizeCount = new int[5];
public static void fillPrizes() {
int count, randInt;
random = new Random();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
count = 0;
while (count == 0) {
randInt = random.nextInt(25);
if (Board.board[randInt] == "[ ]") {
Board.board[randInt] = "[" + prizes[j] + "]";
count++;
}
}
}
}
}
public static void checkPrizes() {
for (int i = 0; i < 25; i++) {
if (Board.board[i].equals("[[Puzzle]]"))
prizeCount[0] ++;
else if (Board.board[i].equals("[[Poster]]"))
prizeCount[1] ++;
else if (Board.board[i].equals("[[Bass]]"))
prizeCount[2] ++;
else if (Board.board[i].equals("[[Game]]"))
prizeCount[3] ++;
else if (Board.board[i].equals("[[Doll]]"))
prizeCount[4] ++;
}
}
}
package penny_pinch_v2;
public class RunPennyPinch {
public static void main(String[] args) {
Board.fillBoard();
Prizes.fillPrizes();
Pennies.throwPennies();
Board.printBoard();
Prizes.checkPrizes();
for (int j = 0; j < 5; j++) {
if (Prizes.prizeCount[j] == 3) {
System.out.println("You won a " + Prizes.prizes[j] + "!");
}
}
}
}
嘿那里,我有我认为的工作代码,但我最近遇到了让控制台显示任何内容的问题。在我添加checkPrizes()方法之前,一切都很好。第二天我尝试运行它,Eclipse会停止响应几秒钟,控制台中不会出现任何内容。如果有人能够澄清什么是错的,(任何逻辑错误,无限循环或Eclipse本身),那将是非常棒的。干杯!
答案 0 :(得分:0)
在这里,您可能想重新考虑在count
方法中增加throwPennies
的位置,或者可能是打破此while
循环的条件。我将其修改如下。
while (count == 0) {
System.out.println("inside throw pennies while");
if (Board.board[randInt].charAt(1) != '[') {
System.out.println("inside throw pennies if");
Board.board[randInt] = "[" + Board.board[randInt] + "]";
}
count++;
}
如果这有帮助,请告诉我。
答案 1 :(得分:-1)
您可以尝试删除checkPrizes()并再次运行它。可能会给出一些更好的理解。或者您可以尝试通过CLI执行。这可以清除问题是否与日食有关。