简体   繁体   English

我不知道为什么我上的课没用

[英]I can't figure out why a class i made isn't working

I just started with objects and classes and when i try to use a method in the class through the .method thing I get a syntax error. 我只是从对象和类开始,当我尝试通过.method方法在类中使用方法时,出现语法错误。 I used almost an identical format for a previous practice problem and it worked fine so I'm drawing a blank here. 对于以前的练习问题,我使用了几乎相同的格式,但效果很好,所以在这里画了一个空白。

this is the class for the object. 这是对象的类。

public class Automobile {
    public double mpg;
    public double gallons = 0;

    public Automobile(double mpg) {
        this.mpg = mpg;
    }

    public void fill(double f) {
        gallons += f;
    }

    public void takeTrip(double m) {
        gallons = gallons - (1 / (mpg / m));
    }

    public void reportFuel() {
        System.out.println(gallons);
    }
}

this is the tester class 这是测试人员类

public class Tester {
Automobile myBmw = new Automobile(24);
 myBmw.fill(20);
 myBmw.takeTrip(100);
 myBmw.reportFuel();
}

As per java syntax, you can't write executable statements as mentioned here: 按照Java语法,您无法编写此处提到的可执行语句:

 myBmw.fill(20);
 myBmw.takeTrip(100);
 myBmw.reportFuel();

out of methods/constructors/blocks. 在方法/构造函数/块之外。 You need to move your code to an appropriate place as per your requirement. 您需要根据需要将代码移动到适当的位置。

You Tester class is trying to execute code outside of a valid execution context (ie a method or static block)... 您的Tester类正在尝试在有效的执行上下文(即methodstatic块)之外执行代码。

public class Tester {
    Automobile myBmw = new Automobile(24);
    public Tester() {
        myBmw.fill(20);
        myBmw.takeTrip(100);
        myBmw.reportFuel();
    }
}

For example... 例如...

You cannot do this in the class like this. 您不能在这样的课程中这样做。 You have to call other class methods from a method/block. 您必须从方法/块中调用其他类方法。

public class Tester {
  Automobile myBmw = new Automobile(24);
  public Tester(){  // It may be any other function 

    myBmw.fill(20);
    myBmw.takeTrip(100);
    myBmw.reportFuel();
  }

  // OR if you miss the main()
  public static void main(String args[]){
    Automobile myBmw = new Automobile(24); 
    myBmw.fill(20);
    myBmw.takeTrip(100);
    myBmw.reportFuel();
  }
}

Change the block 换块

public class Tester {
Automobile myBmw = new Automobile(24);
 myBmw.fill(20);
 myBmw.takeTrip(100);
 myBmw.reportFuel();
}

to

public class Tester {
Automobile myBmw = new Automobile(24);
public Tester(){
 myBmw.fill(20);
 myBmw.takeTrip(100);
 myBmw.reportFuel();
}
}

or place it inside another method in Tester class or a static { } block(you will need to declare Automobile instance as static too for this) 或将其放置在Tester类的另一个方法中或放置在静态{}块中(为此您也需要将Automobile实例声明为静态)

You should use main class , I have executed your code and its running fine when we use main class 您应该使用主类,当我们使用主类时,我已经执行了您的代码并且运行良好

public class Tester {
public static void main(String[] args) {
    Automobile myBmw = new Automobile(24);
     myBmw.fill(20);
     myBmw.takeTrip(100);
     myBmw.reportFuel();
}
}

And if you dont want to use main() , then you should use a method for it 并且,如果您不想使用main(),那么应该使用一种方法

public void testME() {
        Automobile myBmw = new Automobile(24);
        myBmw.fill(20.0);
        myBmw.takeTrip(100);
        myBmw.reportFuel();
    }

You did not provide main method in your tester class so that you got error. 您没有在测试器类中提供main方法,从而导致错误。

public class Tester {
public static void main(String args[]){ // main method required.
Automobile myBmw = new Automobile(24);
 myBmw.fill(20);
 myBmw.takeTrip(100);
 myBmw.reportFuel();
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM