这里才刚刚开始。我以前上过编程课程,但我是Java新手,没有丰富的经验。这个节目来自梅赫兰·萨哈米在斯坦福大学的演讲,并上传到Youtube上。https://www.youtube.com/watch?v=YpZCKVG4s5k&t=1996s从大约32分钟开始,代码是可见的。这是一个简单的图形程序,显示了一个弹跳球。对我来说,这是一个开始尝试设置的好地方,用一个对象替换另一个对象,并总体上习惯于语法如何与屏幕上显示的内容相关。但是,我甚至不能到达比喻的起跑门!我尝试剪切并粘贴到Sololearn仿真器中,但得到了相同的错误。我认为这一定与acm库有关,但是。。。什么?
下面是代码,下面是错误消息。
import acm.program.*;
import acm.graphics.*;
public class BouncingBall extends GraphicsProgram {
private static final int DIAM_BALL = 30;
private static final double GRAVITY = 3;
private static final int DELAY = 50;
private static final double X_START = DIAM_BALL / 2;
private static final double Y_START = 100;
//x velocity
private static final double X_VEL = 5;
//Y velocity determined by gravity and bounce
private static final double BOUNCE_REDUCE = 0.9;
//Starting coords
private double xVel = X_VEL;
private double yVel = 0.0;
//private instance variable
private GOval ball;
}
public void run(){
setup(){
while (ball.getX() < getWidth()) {
moveBall();
checkForCollision();
pause(DELAY);
}
}
//create and place ball
private void setup(){
ball=new GOval(X_START,Y_START,DIAM_BALL,DIAM_BALL);
ball.setFilled(true);
add(ball);
}
//update and move ball
private void moveBall(){
yVel+=GRAVITY;
ball.move(xVel,yVel);
}
//Collision detection
private void checkForCollision(){
if(ball.getY()>getHeight()-DIAM_BALL){
yVel=-yVel*BOUNCE_REDUCE;
double diff=ball.getY()-(getHeight()-DIAM_BALL);
ball.move(0,-2*diff);
}
}
}
}
"Error: Java: class,interface,or enum expected“大约有12个,指定(22,12),(26,17),(27,17),(28,13),(33,13),(34,13)。。。
我有一种感觉,当我明白为什么其中一些是一个问题时,我将能够解决所有这些问题。
提前感谢!
发布于 2017-08-04 02:44:24
我在文件末尾删除了setup(){和a},我认为这是问题的原因。
import acm.program.*;
import acm.graphics.*;
public class BouncingBall extends GraphicsProgram {
private static final int DIAM_BALL = 30;
private static final double GRAVITY = 3;
private static final int DELAY = 50;
private static final double X_START = DIAM_BALL / 2;
private static final double Y_START = 100;
//x velocity
private static final double X_VEL = 5;
//Y velocity determined by gravity and bounce
private static final double BOUNCE_REDUCE = 0.9;
//Starting coords
private double xVel = X_VEL;
private double yVel = 0.0;
//private instance variable
private GOval ball;
public void run(){
setup();
while (ball.getX() < getWidth()) {
moveBall();
checkForCollision();
pause(DELAY);
}
}
//create and place ball
private void setup(){
ball=new GOval(X_START,Y_START,DIAM_BALL,DIAM_BALL);
ball.setFilled(true);
add(ball);
}
//update and move ball
private void moveBall(){
yVel+=GRAVITY;
ball.move(xVel,yVel);
}
//Collision detection
private void checkForCollision(){
if(ball.getY()>getHeight()-DIAM_BALL){
yVel=-yVel*BOUNCE_REDUCE;
double diff=ball.getY()-(getHeight()-DIAM_BALL);
ball.move(0,-2*diff);
}
}
}
查看35:03的视频,它准确地显示了上面的内容。
https://stackoverflow.com/questions/45490655
复制相似问题