我想编写一个在窗口上绘制一些特定点的代码。我有3个不同点$(e1,e11),(e2,e22),(e3,e33)$。我想随机选择其中一个,并根据输出,绘制一个不同的点。
public class ChaosGame {
public static void main(String[] args) {
Window window = new Window("Chaos", 800, 800);
window.open();
int e1 = 420;
int e11 = 170;
int e2 = 230;
int e22 = 670;
int e3 = 700;
int e33 = 540;
while (window.isOpen()) {// ändere den Fensterinhalt
int pos1 = 100;
int pos2 = 300;
int i = (int) (Math.random() * 3);
if (i == 0 || i == 3) {
window.fillRect(pos1 + e1 / 2, pos2 + e11 / 2, 5, 5);
pos1 = e1 / 2;
pos2 = e11 / 2;
window.refresh();
} else if (i == 1) {
window.fillRect(pos1 + e2 / 2, pos2 + e22 / 2, 5, 5);
pos1 = e2 / 2;
pos2 = e22 / 2;
window.refresh();
} else {
window.fillRect(pos1 + e3 / 2, pos2 + e33 / 2, 5, 5);
pos1 = e3 / 2;
pos2 = e33 / 2;
window.refresh();
}
}
}
}
fillRect在新窗口中绘制我的点数。现在当我运行这个程序时,只出现两个点,即使它应该继续“while window.isOpen”,所以直到我关闭窗口。我觉得我的Math.random()有些不对劲。
答案 0 :(得分:0)
您需要在window.fillRect(pos1 + e2 / 2, pos2 + e22 / 2, 5, 5);
声明中将window.fillRect(pos1 + e3 / 2, pos2 + e33 / 2, 5, 5);
更改为else
。你在两种情况下绘制相同的东西。