我正在尝试创建课程并且已经部分成功。但是当我尝试在我的作品中创建东西时,我main method
所做的几乎所有事情都会出现这些错误。如果我改为在其他方法中创建这些东西,然后从我的 main 方法中调用它们,它就可以工作。
但这不是我应该做的,所以我需要修复它,以便主要方法中的内容有效。我得到的第一个错误是当我尝试在我的主对象中创建对象 turtle t1 时,我得到了错误Cannot make a static reference to the non-static field Ystart
import java.util.Random;
import se.lth.cs.window.SimpleWindow;
public class Racetrack {
Random rand = new Random();
int i = 0;
int x;
int Ystart;
int Ygoal;
int RaceTrack;
//SimpleWindow w = new SimpleWindow(500, 500, "22niggs");
public static void main(String[]args) {
SimpleWindow w = new SimpleWindow(500, 500, "Angels");
Racetrack race = new Racetrack(100,400);
race.draw(w);
Turtle t1 = new Turtle(w,50,Ystart);
Turtle t2 = new Turtle(w,400,Ystart);
t1.penDown();
t2.penDown();
while(i==0){
x=rand.nextInt(10);
t1.forward(x);
x = rand.nextInt(5);
t2.forward(x);
SimpleWindow.delay(100 );
}
}
public Racetrack(int Ystart, int Ygoal){ //KONSTRUKTOR
this.Ystart =Ystart;
this.Ygoal = Ygoal;
}
public void draw(SimpleWindow w){
w.moveTo(50, Ystart);
w.lineTo(450, Ystart);
w.moveTo(50, Ygoal);
w.lineTo(450, Ygoal);
}
public void RacingEvent(Racetrack track, Turtle t1, Turtle t2){
}