子类不允许世界运行

时间:2021-08-08 21:45:16

I am a beginner, taking a CompSci class in school. This chapter is making subclasses; however, The class is somehow causing the program to terminate. The world doesn't even appear, and there are no errors. (I am running Eclipse).

我是初学者,在学校参加CompSci课程。本章正在制作子类;但是,该类以某种方式导致程序终止。世界甚至没有出现,也没有错误。 (我正在运行Eclipse)。

Here is my code:

这是我的代码:

package karel;
import kareltherobot.*;
public class Race implements Directions
{
public static void main(String args[]) {



class Car extends UrRobot
{   public Car(int street, int avenue, Direction direction, int beepers) 
    {super(5, 5, East, infinity);
    }

    public void turnAround()
    {
        turnLeft();
        turnLeft();
    }

    public void turnRight()
    {   
        turnLeft();
        turnLeft();
        turnLeft();
    }
}
    {   
        World.setVisible(true);
        World.showSpeedControl(true);
    }
        {
            Car kar = (Car) new UrRobot(5, 5, East, infinity);
            kar.move();
            kar.turnLeft();
            kar.move();
            kar.turnAround();
            kar.move();
            kar.turnRight();

        }

}

}

Is there a way to get this program to not error to termination?

有没有办法让这个程序没有错误终止?

2 个解决方案

#1


0  

You need to have code ran in the main function. Java finds and executes code in the classes main function. Try that, if it doesn't work, come back and repost.

您需要在main函数中运行代码。 Java在类main函数中查找并执行代码。试试看,如果它不起作用,请回来重新发布。

#2


0  

Try the following.

请尝试以下方法。

import kareltherobot.*;

public class Race implements Directions
{
    public Race(){
     }

public static void main(String args[]) {

class Car extends UrRobot
{   

public Car(int street, int avenue, Direction direction, int beepers)

{

    super(street, avenue, direction, beepers);
}

public void turnAround()

{
    super.turnLeft();
    super.turnLeft();
}
public void turnRight()
{           
    super.turnLeft();
    super.turnLeft();
    super.turnLeft();
}
}

World.setVisible(true);
World.showSpeedControl(true);    
Car kar = new Car(5, 5, East, 100);
kar.move();
kar.turnLeft();
kar.move();
kar.turnAround();
kar.move();
kar.turnRight();

}
}

#1


0  

You need to have code ran in the main function. Java finds and executes code in the classes main function. Try that, if it doesn't work, come back and repost.

您需要在main函数中运行代码。 Java在类main函数中查找并执行代码。试试看,如果它不起作用,请回来重新发布。

#2


0  

Try the following.

请尝试以下方法。

import kareltherobot.*;

public class Race implements Directions
{
    public Race(){
     }

public static void main(String args[]) {

class Car extends UrRobot
{   

public Car(int street, int avenue, Direction direction, int beepers)

{

    super(street, avenue, direction, beepers);
}

public void turnAround()

{
    super.turnLeft();
    super.turnLeft();
}
public void turnRight()
{           
    super.turnLeft();
    super.turnLeft();
    super.turnLeft();
}
}

World.setVisible(true);
World.showSpeedControl(true);    
Car kar = new Car(5, 5, East, 100);
kar.move();
kar.turnLeft();
kar.move();
kar.turnAround();
kar.move();
kar.turnRight();

}
}