当我从Java的第4章:初学者指南中运行此代码时,我收到以下错误消息。我真的想要理解这段代码,如果我知道它有什么问题,它会有很大的帮助。
错误:在类Vehicle中找不到主要方法,请将主方法定义为: public static void main(String [] args) 或者JavaFX应用程序类必须扩展javafx.application.Application
以下是从发布商(McGraw-Hill)网站下载的代码:
// Add a constructor.
class Vehicle {
int passengers; // number of passengers
int fuelcap; // fuel capacity in gallons
int mpg; // fuel consumption in miles per gallon
// This is a constructor for Vehicle.
Vehicle(int p, int f, int m) {
passengers = p;
fuelcap = f;
mpg = m;
}
// Return the range.
int range() {
return mpg * fuelcap;
}
// Compute fuel needed for a given distance.
double fuelneeded(int miles) {
return (double) miles / mpg;
}
}
class VehConsDemo {
public static void main(String args[]) {
// construct complete vehicles
Vehicle minivan = new Vehicle(7, 16, 21);
Vehicle sportscar = new Vehicle(2, 14, 12);
double gallons;
int dist = 252;
gallons = minivan.fuelneeded(dist);
System.out.println("To go " + dist + " miles minivan needs " +
gallons + " gallons of fuel.");
gallons = sportscar.fuelneeded(dist);
System.out.println("To go " + dist + " miles sportscar needs " +
gallons + " gallons of fuel.");
}
}
答案 0 :(得分:0)
将“类VehConsDemo”更改为“ public 类VehConsDemo”,确保将文件保存为VehConsDemo.java并编译并运行它。
由于课程不公开,您可能无法运行它。