如果Java的油漆X =位置做别人做 [英] Java Paint if x = location do this else do this
问题描述
我在电脑SCI AP和我想不通这是为什么不工作
I'm in computer SCI AP and I can't figure out why this isn't working
public static void moon(Graphics g) {
for(int k = 0; k < 550; k+=2) {
if (k == 550) {
g.setColor(Color.blue);
g.fillOval(k,50,50,50);
for(long delay = 1; delay<10000000; delay++);
g.setColor(Color.white);
g.fillOval(k,50,50,50);
k = 0;
} else {
g.setColor(Color.black);
g.fillOval(k,50,50,50);
for(long delay = 1; delay<10000000; delay++);
g.setColor(Color.white);
g.fillOval(k,50,50,50);
}
}
}
所以基本上是将在屏幕上的黑色椭圆形移动,然后当 K
= 550这将使在屏幕上蓝色的去..但事实并非如此这样做,它只是移动黑色的,然后它击中550后停止..和蓝某不上来。
So basically is will make the black oval move across the screen then when k
= 550 it will make the blue one go across the screen.. But it doesn't do that it just moves the black one then after it hits 550 it stops.. and the blue one doesn't come up.
推荐答案
您code从未达到K = 550结果
在为
循环的条件是 K&LT; 550
。让 K&LT; = 550
,它应该工作
Your code never reaches k = 550.
The condition in the for
loop is k < 550
. Make it k <= 550
and it should work.
不过,你可能看不到这一点,因为你使用为(长延迟= 1;延迟&LT;千万;延迟++);
延迟 - 这是不是一个非常可靠的方法来延迟。
However, you may not see it happen, because you're using for(long delay = 1; delay<10000000; delay++);
to delay - this is not a very reliable way to delay.
看东西更可靠地发生,请尝试使用的视频下载(长)。结果
为了记录在案,我相信有针对一般使用视频下载(长)
一些参数,但在这种情况下,它应该足够了。
To see things happen more reliably, try using Thread.sleep(long).
For the record, I believe there are some arguments against using Thread.sleep(long)
in general, but in this case it should suffice.
而我在它的 K = 0
分配是多余的,它甚至不良作风。你不应该分配给一个循环变量的在的循环。如果循环再次进入时,的for(int k = 0
部分将设置K为0的照顾。
While I'm at it, the k = 0
assignment is superfluous, it's even bad style. You should not assign to a loop variable inside the loop. If the loop is entered again, the for (int k=0
part will take care of setting k to 0.
心连心
这篇关于如果Java的油漆X =位置做别人做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!