2013-02-03 55 views
0

林要去跟與我的車跑的編譯器的幫助,我chaned一些東西,我有1個錯誤試圖實現一個類

public class AutomobileDescription 
{ 
    /** 
    Constructor to display the make, model and price the new automobile I wish to purchase 
    */ 

    public AutomobileDescription(String carMake, String carModel, carPrice) 
    { 
     make = m; 
     model = mo; 
     price = p; 
    } 
    public String m =("Toyota"); 
    public String mo =("Camry"); 
    public String p =("22055"); 

    public String getAutomobileinfo() 
    { 
    return m + mo + p; 
    Automobile myAutomobile = new Automobile(Toyota, Camry, 22055); 
    System.out.println("The Make, Model and Price of the car is: m + mo + p "); 

    } 
} 

---- jGRASP高管:javac的-g AutomobileDescription.java

AutomobileDescription.java:7:錯誤:預期 公共AutomobileDescription(字符串carMake,字符串carModel,carPrice) ^ 1錯誤

---- jGRASP wedge2:退出代碼FO r進程爲1. ---- jGRASP:操作完成。

+0

你有問題嗎? – SLaks

+2

方法應該有名稱,只能有字母,數字或下劃線。 – SLaks

+0

此外,您必須提供執行'getMake','getModel'和'getPrice'。 – Vulcan

回答

1

您在這裏有許多問題:

public class AutomobileDescription 
{ 
    /** 
    Constructor to display the make, model and price the new automobile I wish to purchase 
    */ 

public AutomobileDescription(String carMake, String carModel, /*no return type*/ carPrice) 
{ 
    make = m; 
    model = mo; 
    price = p; 
} 
public String m =("Toyota"); 
public String mo =("Camry"); 
public String p =("22055"); 

    public String getAutomobileinfo() 
    { 
    return m + mo + p; /*return? then why statements after this?*/ 
    Automobile myAutomobile = new Automobile(Toyota, Camry, 22055); 
    System.out.println("The Make, Model and Price of the car is: m + mo + p "); 

    } 
} 

解決方案:

public class AutomobileDescription{ 
/** 
Constructor to display the make, model and price the new automobile I wish to purchase 
*/ 

public AutomobileDescription(String carMake, String carModel, String carPrice) 
{ 
    m = make; 
    mo = model; 
    p = carPrice; 
} 
private String m; 
private String mo; 
private String p; 

public String getAutomobileinfo() 
{ 
    return m + mo + p; 
} 
public static void main(String[] args){ 
    AutomobileDescription myAutomobile = new AutomobileDescription("Toyota", "Camry", "22055"); 
    System.out.println("The Make, Model and Price of the car is: " + myAutomobile.getAutomobileinfo()); 
} 
} 
+0

謝謝你的幫助很多 –

+0

酷@shepcleveland - 得到一本關於Java的好書:-) ..並破解你的方式。 –

0

這不是一個有效的方法名稱:

public String getMake + getModel + getPrice; 

解決這個問題。如果你仍然有問題,請稍微詳細一點。甚至可能發佈錯誤信息!

+0

老實說,它確實顯示一點點。至於我的回答,一個方法名稱不能有'+'這樣的符號。你可以說'公共字符串getAllDetails {'和那應該工作。這仍然不意味着那裏沒有更多的錯誤。 – mrunion

1
public AutomobileDescription(String carMake, String carModel, carPrice) 
                   ^^^^^^^^ 

您省略了參數carPrice類型。最有可能你想

public AutomobileDescription(String carMake, String carModel, BigDecimal carPrice) 

另一個問題......

public String getAutomobileinfo() 
{ 
    return m + mo + p; 
    Automobile myAutomobile = new Automobile(Toyota, Camry, 22055); 
    System.out.println("The Make, Model and Price of the car is: m + mo + p "); 
} 

return聲明意味着兩個下面的語句永遠不能達到,這將導致編譯錯誤更正後的第一個問題。

+0

我已經提到了完全相同的東西:-P ... –

+0

如果你看到'汽車(豐田,凱美瑞,22055);'也有問題..什麼是汽車?什麼是豐田,凱美瑞是什麼? 22055不是一個字符串.. ?? OP似乎完全不知道他在做什麼。 –

+0

你和我在同一時間寫我們的答案。檢查時間戳。 –