刚刚开始。我之前有过编程课程,但我是Java的新手,没有丰富的经验。该课程来自Mehran Sahami的斯坦福大学讲座,发布在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);
}
}
}
}
&#34;错误:Java:期望的类,接口或枚举&#34; 其中大约有十几种,具体说明(22,12),(26,17),(27,17),(28,13),(33,13),(34,13)。 。 。
我有一种感觉,当我理解为什么其中一些是问题时,我能够解决所有这些问题。
提前致谢!
答案 0 :(得分:0)
我删除了文件末尾的setup(){和}},我认为这是问题的原因。
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查看视频,它会准确显示上面的内容。
答案 1 :(得分:0)
@Saint Razz:首先,您使用的是非公共图书馆&#39; acm.jar&#39;和Sololearn模拟器
你不懂这个图书馆。
其次,尽量避免使用静态&#39;。除了你编写一个类,它有方法,变量,常量只能存在一次。
e.g。
class Student() {
private String mName;
private String mCollege;
public Student(String name) {
mName = name;
college = "ITS";
}
}
在这种情况下,将内存问题用于String大学的静态是有意义的。
接下来有一个错误,Berkley Lamb也纠正了。在方法中声明方法是没有意义的。我的意思是run()方法中的setup()方法。
仔细查看大括号。如果尝试在java中调用类之外的方法,编译器将始终抛出错误。 (参见私人GOval球后的近距离大括号 - 宣言。
最后我建议你在窗口宽度和高度上使用常量,或者在调用run方法之前在public void init(){}方法中初始化窗口,只要你使用acm。罐。否则,您可能会调用getWidth()并获取值0.这会导致一些不必要的错误。
我希望它能进一步帮助你。