gpt4 book ai didi

java - 如何使用For循环和While循环来重复代码块

转载 作者:太空宇宙 更新时间:2023-11-04 09:19:10 26 4
gpt4 key购买 nike

我需要使用 for 循环创建第二个 if 语句(按下按钮时)重复命令 5 次,然后使用 while 循环创建第三个 if 语句

while 和 for 循环是我尝试过的东西,但我还没有让代码工作。它只在随机位置绘制一个点。

    if (mouseX >= BUTTONX && mouseX <= BUTTONX+BUTTONWIDTH && mouseY >= BUTTONY && mouseY <= BUTTONY+BUTTONHEIGHT) {
//if click on button, choose random position
x = (int)random(width);
y = (int)random(height);
} else {
if (mouseX >= BUTTONX && mouseX <= BUTTONX+BUTTONWIDTH && mouseY >= BUTTONY2 && mouseY <= BUTTONY2+BUTTONHEIGHT) {
for(int a = 0; a <= 5; a++){
x = (int)random(width);
y = (int)random(height);
}
} else {
if (mouseX >= BUTTONX && mouseX <= BUTTONX+BUTTONWIDTH && mouseY >= BUTTONY3 && mouseY <= BUTTONY3+BUTTONHEIGHT) {
int i = 0;
while(i <= 5){
x = (int)random(width);
y = (int)random(height);
i++;
}
} else {
//if click off button, place dot at mouse location
x = mouseX;
y = mouseY;
}
}
}
}

最佳答案

您正在计算 x 和 y 的多个值,但您只会使用最终(第 5 个)值,因为循环中没有任何内容可以使用它们

关于java - 如何使用For循环和While循环来重复代码块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58564568/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com