首页 文章

Java应用程序不显示输出

提问于
浏览
0

这是我更新的代码:

package car1;

公共类Main {

public static void main(String [] args){

class HondaCivic实现car1 {

int speed = 0;
int rpm = 0;
int gear = 1;

public void speedUp(int Increment) {
    speed = speed + Increment;}

public void applyBrakes(int Decrement) {
    speed = speed - Decrement;}

public void changeRpm(int NewValue) {
    rpm = NewValue;}

public void changeGear(int NewValue) {
    gear = NewValue;}

public void printStates() {
    System.out.println("speed:"+speed+" rpm:"+rpm+" gear:"+gear);}

}

class CarDemo{
public void main(String[] args) {
    // Two different Cars
    HondaCivic car1 = new HondaCivic();
    HondaCivic car2 = new HondaCivic();
    // Methods for cars
    car1.speedUp(30);
    car1.changeGear(3);
    car1.changeRpm(3000);
    car1.applyBrakes(15);
    car1.printStates();

    car2.speedUp(30);
    car2.changeGear(3);
    car2.changeRpm(2000);
    car2.applyBrakes(15);
    car2.speedUp(5);
    car2.changeGear(1);
    car2.printStates();
}

}}}

应用程序不会显示输出 . 我不知道该怎么做 . 有什么建议?

5 回答

  • 0

    与大多数编程语言一样,Java区分大小写 . Classclass 不是一回事 .

  • 1

    Java区分大小写:

    Class HondaCivic implements Car {
    

    与法律语法不同:

    class HondaCivic implements Car {
    
  • 3

    接口需要从其父级实现所有方法 . 你正在实现除了之外的一切

    printStates()
    

    另外,检查类声明的区分大小写 .

    编辑:nvm未声明为抽象

  • 1

    你的代码有很多问题 .

    首先让Car成为像 interface Car 这样的界面

    第二步将所有代码从 HondaCivic 移动到 Car ,将所有代码从 Car 移动到 HondaCivic ,即交换代码,因为接口只能有方法声明和变量而不能实现 . 实现接口的类需要提供所有这些方法的实现 .

    最后在main方法中编写此代码而不是用于制作Car实例的代码

    Car car1 = new HondaCivic();
    Car car2 = new HondaCivic();
    

    然后它将编译并运行 .

  • 0

    我可以发现一些问题,其中一些问题可能已经修复过:

    • Class 需要在第二个类的定义中为 class (小写) .

    • Car 不是接口,因此您必须 extend 而不是实现它 .

    • HondaCivic 不是抽象的或接口,因此它必须为每个方法都有方法体;或者,将方法保留在HondaCivic之外,在这种情况下,将使用 Car 的方法 .

    在你当前的类布局中,如果制作一个Honda Civic对象而不是一个类,它会更有意义,因为它没有新的功能:

    Car hondaCivic = new Car();
    

相关问题