2013-08-16 74 views
-4

當談到我缺乏Java技能時,我仍然處於HelloWorld。我無法理解這是爲了挽救我的生命。我正在寫一個覆蓋方法?在我開始嘗試完成其餘的課程之前,我剛剛開始使用我的學校提供​​的代碼,而且我已經發現了一些我甚至無法想到如何糾正的錯誤。任何幫助都將不勝感激。我只是希望這個錯誤消失,所以我可以創造一些新的:) 下面是代碼:錯誤:無法找到或加載主類車輛。車輛

public class Vehicle 
public static void main (String [] args)} 
{ 
private boolean moving; // whether or not the vehicle 
private double speed; 
private char bearing; 
('N','E','S', or 'W') 
public Vehicle(){ // Vehicle class no-arg constructor 
    moving = false; // assume not moving 
    speed = 0.0; // not moving 
    bearing = 'N'; // assume 'N'orth 
    System.out.println("Created a vehicle (no-arg)"); 
} 
public Vehicle (double initialSpeed) // Vehicle 1-arg constructor 
    bearing = 'W'; 
    speed = initialSpeed; 
    if (speed > 0.0) 
    { 
     moving = true; 
    } 
    System.out.println("Created a vehicle (1-arg)"); 
public Vehicle (double initialSpeed, char initialBearing) // Vehicle 2-arg constructor 
    bearing = initialBearing; 
    speed = initialSpeed; 
    if (speed > 0.0){ 
     moving = true; 
    } 
    System.out.println("Created a vehicle (2-arg)"); 
public void start(double initialSpeed, char initialBearing){ 
    moving = true; 
    if (initialSpeed >= 5.0 && initialSpeed <= 20.0){ 
     speed = initialSpeed; // valid expected range 
    } else if (initialSpeed >= 0.0 && initialSpeed < 5.0){ 
     speed = 5.0; // minimum 
    } else if (initialSpeed < 0.0){ 
     speed = 0.0; // assume no movement 
     moving = false; 
    } else if (initialSpeed > 20.0){ 
     speed = 20.0; // maximum allowed 
    } 
    switch(initialBearing){ 
    case 'N': 
     bearing = initialBearing; 
     break; 
    case 'E': 
     bearing = initialBearing; 
     break; 
    case 'S': 
     bearing = initialBearing; 
     break; 
    case 'W': 
     bearing = initialBearing; 
    default: 
System.out.println("invalid bearing " + 
    initialBearing + 
    " set to N"); // additional user notification 
    bearing = 'N'; 
} 
public double getSpeed() { // get and return current speed in mph 
    return speed; 
} 
public void setSpeed(double newSpeed){ // set new speed in mph 
    speed = newSpeed; 
} 
    /** 
    * 
    * @return 
    */ 
public char getBearing(){ 
    return bearing; 
} 
public void speedUp(double mphSteps, int numSteps){ 
    int counter = 0; 
    while (counter < numSteps) 
    speed += mphSteps; 
    System.out.println("counter= " + counter + ", " + 
    this.toString()); 
    counter++; 
} 
public String toString(){ 
    return "From toString(): speed= " + getSpeed() + 
    " mph and bearing= " + getBearing(); 
} 
} 
public class Car extends Vehicle{ 
    private String color; 
    private int doors; 
    private double hp; 
    public Car(String carColor, int numDoors, 
    double horsePower, double 
    startingSpeed) 
{ 
super(startingSpeed); 
color = carColor; 
doors = numDoors; 
hp = horsePower; 
System.out.println("Created a car"); 
} 
public String getColor() 
{ 
return color; 
} 
public int getDoors() 
{ 
return doors; 
} 
public double getHp() 
{ 
return hp; 
} 
public String toString() 
{ 
return "From Car toString(): color= " + getColor() + 
" doors= " + getDoors() + 
" hp= " + getHp() + 
" speed= " + getSpeed() + 
" mph and bearing= " + getBearing();} 
} 
public class TestCar2 
{ 
public static void main(String[] args) 
{ 
Car myCar2 = new Car("blue", 4, 300., 10.0); 
System.out.println(myCar2.toString()); 
myCar2.speedUp(5.0, 2); 
} 
} 

請請,謝謝您的幫助!

+0

是否在包裝車輛下?這是什麼公共靜態無效的主要(字符串[] args)} ? –

+0

該代碼是否可以編譯? –

+2

如果你:1)**縮進**代碼。 2)讀取代碼中的錯誤(由編譯器在消息中引發)。 3)如果您不瞭解錯誤,請發佈它們,我們將引導您。請注意,這不是*爲我*網站做家庭作業/練習,但看起來你真的想學習,所以請成爲一個好學生,併爲我們提供我們需要引導你的東西。 –

回答

1
public class Vehicle 

public static void main (String [] args)} // this is totally wrong 
    // This not compile at least 
{ 

實際上你使用的是parenthesis({})是完全錯誤的。以下結構應該跟着你。我猜測你沒有使用IDE來編寫代碼。我建議你使用IDE來做代碼。

public class MyClass{ 

public static void main(String[] args){ 
    // main method 
} 
// some other method 

} 
0

首先你不能在一個文件中有兩個公共類!

其次,你的括號完全不匹配。

public class Vehicle 
public static void main (String [] args)} 
{ 

類有它自己的作用域,main()函數有它自己的作用域。因此,將您的代碼更改爲

public class Vehicle { 
public static void main (String [] args) { //your code} 
} 

您的多個arg構造函數沒有正確的括號。

public Vehicle (double initialSpeed) // Vehicle 1-arg constructor 
    bearing = 'W'; 
    speed = initialSpeed; 
    if (speed > 0.0) 
    { 
     moving = true; 
    } 
    System.out.println("Created a vehicle (1-arg)"); 

將其更改爲

public Vehicle (double initialSpeed) { // Vehicle 1-arg constructor 
    bearing = 'W'; 
    speed = initialSpeed; 
    if (speed > 0.0) 
    { 
     moving = true; 
    } 
    System.out.println("Created a vehicle (1-arg)"); 
} 

最後你的switch語句,你需要把斷在所有情況下(除了默認) 變化

case 'W': 
bearing = initialBearing; 

case 'W': 
bearing = initialBearing; 
break; 

如果你對於java和學習基礎知識都是新手,我建議使用Eclipse,Netbeans或Intellij IDEA之類的IDE。谷歌他們找到更多的信息。

+0

我會改變你說的一切!非常感謝您的幫助。我像你說的那樣使用Netbeans!再次感謝你。 –

+0

返回獲得更多幫助, –

+0

在代碼中,我有公共類車輛,然後我有公共類汽車延伸車輛..我想添加代碼公共類TestCar 2的結尾,我得到錯誤的主要方法找不到。知道我在猜測TestCar2無法從Car中擴展,因爲如果我只是想擴展它,爲什麼還要寫Car?但我的任務需要我修改TestCar2並製作TestCar3。我有一本Java書,可能用普通話寫成。我只是想了解所有這一切。是否有任何人可以推薦新手的書或網站? –