我已经开始学习Java课程了,所以我很新,我得到了一个“Bouncing balls”的任务。我必须这样做才能让用户输入他/她想要在屏幕上看到的球数。我试过做循环,但我完全确定我做错了,因为我一直看到一个球。任何人都可以给我一些指示/提示/或表明问题吗?谢谢:))
程序:
public class BouncingBall {
public static void main(String[] args) {
int N = StdIn.readInt();
for(int i=0;i<N;i++){
// set the scale of the coordinate system
StdDraw.setXscale(-1.0, 1.0);
StdDraw.setYscale(-1.0, 1.0);
// initial values
double rx = Math.random();
double ry = Math.random(); // position
double vx = 0.015, vy = 0.023; // velocity
double radius = 0.05; // radius
// main animation loop
while (true) {
// bounce off wall according to law of elastic collision
// for (int i = 0; i < N; i++) {
if (Math.abs(rx + vx) > 1.0 - radius) vx = -vx;
if (Math.abs(ry + vy) > 1.0 - radius) vy = -vy;
// update position
// for (int i = 0; i < N; i++) {
rx = rx + vx;
ry = ry + vy;
// clear the background
StdDraw.clear(StdDraw.GRAY);
// draw ball on the screen
// for (int i = 0; i < N; i++) {
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.filledCircle(rx, ry, radius);
// display and pause for 20 ms
// for (int i = 0; i < N; i++) {
StdDraw.show(20);
}
}
}
}
答案 0 :(得分:0)
我假设你希望N
成为这里的球数。你的问题是第3行的循环跨越整个程序,包括动画循环。因此,不是产生N个球,而是每次接下来N次运行你的游戏。