0
我的程序是创建一个弹跳球,每上升和下降并上升30%向下......告诉球已停在休息位置。使用while循环弹跳球时,我需要减少每次跳跃30%的弹跳直到0,然后退出循环以避免无限循环
另外我想让球逐渐减速,因为它达到了最高点,随着它回落到原来的位置逐渐加速。
所以我得到了第一部分的设置,我只是遇到了没有制造无限循环的麻烦,并且在每次反弹之后减少上限30%。
当我写这个问题时,我意识到,我需要在第一个while循环增加30%门楣时使y值达到400。
我该如何在两个循环周围重复一遍又一遍,没有无限循环?
我很欣赏任何意见或建议!
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JApplet;
public class MY_Proj04 extends JApplet
{
int x, y;
Color Background;
public void init()
{
x = 100;
y = 400;
Background = getBackground();
}
public void paint(Graphics g)
{
// I tryed putting a while loop around the two following while loops and
// doing y = y * 30/100, I did this because the fill oval can't take a double
// as one of its parameters.
// 1st while loop
while(y >= 0) // Ball goes up to (0,100)
{
g.setColor(Background);
// fill the 500 by 500 square with it background color
// any old draw will be covered
g.fillRect(0, 0, 500, 500);
g.setColor(Color.red);
g.fillOval(x, y, 50, 50);
for(long i = 1; i < 5000000; i++); //speed of ball
y -=1;
}
// 2nd while loop
while(y <= 400) // ball goes down to 400,100
{
g.setColor(Background);
// fill the 500 by 500 square with it background color
// any old draw will be covered
g.fillRect(0, 0, 500, 500);
g.setColor(Color.red);
g.fillOval(x, y, 50, 50);
for(long i = 1; i < 5000000; i++); //speed of ball
y += 1;
}
}
}
看看[这个例子](http://stackoverflow.com/questions/19626338/japplet-creates-a-ball-that-bounces-and-gets -progressively-不太高在-java的/ 19626396#19626396) – MadProgrammer 2014-09-10 23:53:37