【发布时间】:2015-03-22 02:53:57
【问题描述】:
我的程序是创建一个弹跳球,每次上下上下负 30%……告诉球已经停在静止位置。
我也想让球在到达弹跳顶部时逐渐减速,并在它下降回到原来的位置时逐渐加速。
所以我设置了第一部分,我只是遇到了不做无限循环的麻烦,并且每次反弹后将向上 y 停止位置减少 30%。
当我写这个问题时,我意识到,我需要使第一个 while 循环中的 y 值增加 30%,它达到 400 正确吗?
如何在没有无限循环的情况下围绕两个 while 循环循环重复?
感谢任何意见、cmets 或想法!
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;
}
}
}
【问题讨论】:
标签: java loops for-loop while-loop