Java 画圈的While循环
到目前为止,这是我以前使用相同的代码创建了一个for循环来创建100个随机圆,但是现在我需要使用while语句,只是有点困惑于使用什么整数和不使用什么整数。我编译它时没有任何错误,但当它运行时,窗口会弹出,其中没有任何内容Java 画圈的While循环,java,loops,while-loop,Java,Loops,While Loop,到目前为止,这是我以前使用相同的代码创建了一个for循环来创建100个随机圆,但是现在我需要使用while语句,只是有点困惑于使用什么整数和不使用什么整数。我编译它时没有任何错误,但当它运行时,窗口会弹出,其中没有任何内容 public void paintComponent(Graphics g) { super.paintComponent(g); Dimension d = getSize(); { int i = 0; while (i<=100)
public void paintComponent(Graphics g) {
super.paintComponent(g);
Dimension d = getSize();
{
int i = 0;
while (i<=100)
{
Color color = new Color(generator.nextInt(255),
generator.nextInt(255), generator.nextInt(255));
g.setColor(color);
int circleSize = generator.nextInt(d.width / 4);
int x = generator.nextInt(d.width - circleSize);
int y = generator.nextInt(d.height - circleSize);
g.fillOval(x, y, circleSize, circleSize);
g.drawArc(x, y, circleSize, circleSize, 0, 360);
}
}
公共组件(图形g){
超级组件(g);
维度d=getSize();
{
int i=0;
虽然(i你在一个无限循环中。你不是在增加i
。将i++;
添加到循环的底部,循环至少应该终止。你在一个无限循环中。你不是在增加i
。将i++;
添加到循环的底部,循环至少应该终止。你忘了增加我的值。nti
在你的while-loop内也要确保你x y保持在边界内。你忘了在while-loop内增加i
也要确保你x y保持在边界内。我真的看到了……谢谢你,虽然它现在起作用了,但我知道我忘记了一些东西。@ZekeP别担心,我们都曾多次被这个bug咬过…:)我真的看到了…谢谢你,虽然它现在起作用了,但我知道我忘了什么。@ZekeP别担心;我们都被这个虫子咬了很多次…:)