2013-10-22 87 views
-5

你好我收到錯誤信息:class interface or enum expected,這是什麼意思?錯誤消息:類接口或枚舉期望,這是什麼意思?

我的代碼:

/** 
* to write a simple java class Mobile that models a mobile phone. 
* 
* @author (john) 
* @version (22/10/13) 
*/ 
public class Mobile 

{ 
    // type of phone 
private String phonetype; 
    // size of screen in inches 
private int screensize; 
    // memory card capacity 
private int memorycardcapacity; 
    // name of present service provider 
private String mobileServiceProvider; 
    // type of contract with service provider 
private int mobileTypeOfContract; 
    // camera resolution in megapixels 
private int cameraresolution; 
    // the percentage of charge left on the phone 
private int chargeUp; 
    // wether the phone has GPS or not 
private int switchedOnFor; 
    // to simulate using phone for a period of time 
private int charge; 
    // checks the phones remaining charge 
private String provider; 
    // simulates changing the provider 
private String GPS; 
    // instance variables - replace the example below with your own 


    // The constructor method 

public Mobile(String mobilephonetype, int mobilescreensize, 
int mobilememorycardcapacity, String mobileServiceProvider, int mobileTypeOfContract, int mobilecameraresolution, String mobileGPS, int chargeUp,int switchedOnFor, String changeProvider,int getBalance) { 


    /** 
*recieves orders for mobiles at the given price. 
    */ 
this(yourCostHere); 


price= 1000; 
balance= 0; 
total= 0; 
} 

/** 
*returns a field price. 
    */ 
public int getPrice() 
    { 
return price; 
    } 

    /** 
    *return the amount of change due for orders of mobiles. 
    */ 
public int getBalance() 
    { 
return balance; 
    }     


     //this.serviceprovider = newserviceprovider; 
     //this.typeofcontract = 12; 
     //this.checkcharge = checkcharge; 
     //this.changeProvider = giffgaff; 

    //Mobile samsungPhone = new Mobile(
// "Samsung" // String mobilephonetype 
//, 1024 // intmobilescreensize 
//, 2  // intmobilememorycardcapacity 
//, 8  // intmobilecameraresolution 
//, "GPS" //String mobileGPS 
//, "verizon" // String newserviceprovider 
//, "100" // intchargeUp 
//, "25" // intswitchedOnFor 
//, "25" // intcheckCharge 
//,  "giffgaff"// String changeProvider 
//); 


     //typeofcontract = 12; 
     //checkcharge = checkcharge; 

    } 
    //Mutator for newserviceprovider 
public void setmobileServiceProvider(String newmobileServiceProvider) 
    { 
mobileServiceProvider = newmobileServiceProvider; 
    } 
    //Mutator for contracttype 
public void setmobileTypeOfContract(int newmobileTypeOfContract) 
    { 
mobileTypeOfContract = newmobileTypeOfContract; 
    } 
    //Mutator for chargeUp 
public void setchargeUp(int chargeUp) 
    { 
chargeUp = chargeUp; 
    } 
    //Mutator to simulate using phone for a period of time 
public void switchedOnFor(int switchedOnFor) 
    { 
switchedOnFor = switchedOnFor; 
    } 
    //Accessor for type of phone 
public String getType() 
    { 
returnphonetype; 
    } 
    //Accessor for provider 
public String getprovider() 
    { 
returnmobileServiceProvider; 
    } 
    //Accessor for contract type 
public int getContractType() 
    { 
returnmobileTypeOfContract; 
    } 
    //Accessor for charge 
public int getCharge() 
    { 
returnchargeUp; 
    } 
    //Accessor which checks the phones remaining charge 
public int checkCharge() 
    { 
returncheckCharge; 
    } 
    // simulates changing the provider 
public void changeProvider() 
    { 
provider = changeProvider 
    } 

public int getBalance() 
    { 
return balance; 
    } 
    // A method to display the state of the object to the screen 
public void displayMobileDetails() { 
System.out.println("phonetype: " + phonetype); 
System.out.println("screensize: " + screensize); 
System.out.println("memorycardcapacity: " + memorycardcapacity); 
System.out.println("cameraresolution: " + cameraresolution); 
System.out.println("GPS: " + GPS); 
System.out.println("mobileServiceProvider: " + mobileServiceProvider); 
System.out.println("mobileTypeOfContract: " + mobileTypeOfContract); 
} 

     /** 
* The mymobile class implements an application that 
* simply displays "new Mobile!" to the standard output. 
*/ 
public class mymobile { 
public void main(String[] args) { 
System.out.println("new Mobile!"); //Display the string. 
    } 
} 
public static void buildPhones(){ 
Mobile Samsung = new Mobile("Samsung",3,4,"verizon",8,12,"GPS",100,25,"giffgaff"); 
Mobile Blackberry = new Mobile("Samsung",3,4,"verizon",8,12,"GPS",100,25,"giffgaff");  
}  
public static void main(String[] args) { 
buildPhones(); 
} 

} 

任何答覆或答覆和幫助,將不勝感激,因爲我不能讓它編譯喜歡它,沒有語法錯誤以前那樣。

+2

哪條線產生該錯誤? – Thomas

+1

此代碼的格式非常難以閱讀。 –

+3

請停止使用大量代碼創建問題,並在您未指定的地方發生錯誤。理想情況下,將代碼縮減爲*只是一個簡短但完整的程序,用於演示問題。 –

回答

1

除了編譯器錯誤,還有其他的錯誤,例如,這一個:

public void switchedOnFor(int switchedOnFor) 
{ 
    switchedOnFor = switchedOnFor; 
} 

這將沒有任何效果,因爲參數switchedOnFor將陰影成員switchedOnFor,即你的參數分配給自己。

要解決這個問題(以及其他人)使用this.switchedOnFor = switchedOnFor;。將參數聲明爲final也許會更好,因此編譯器可以幫助您,因爲該分配不再編譯。

編輯:我將總結評論中報告的錯誤,所以請不要爲他們加倍努力。這只是爲了提供一個簡潔的答案。

JonSkeet發現基本誤差:

public class Mobile 
{ 
    ... 
} 
//Mutator for newserviceprovider 
public void setmobileServiceProvider(String newmobileServiceProvider) { ... 

大括號只是方法之前關閉類,因此該方法被定義一個類,接口或枚舉之外,這就是編譯器會告訴你。

Raghunandan發現這一個:

public String getprovider() 
{ 
    returnmobileServiceProvider; 
} 

編譯器會抱怨returnmobileServiceProvider暫時不明以及缺少return語句。在這裏,缺少一個簡單的空間。

的另一個問題是你的構造(如暗示的Raghunandan):這裏

public Mobile(String mobilephonetype, int mobilescreensize, int mobilememorycardcapacity, String mobileServiceProvider, int mobileTypeOfContract, int mobilecameraresolution, String mobileGPS, int chargeUp,int switchedOnFor, String changeProvider,int getBalance) { 

    /** 
    *recieves orders for mobiles at the given price. 
    */ 
    this(yourCostHere); 

    price= 1000; 
    balance= 0; 
    total= 0; 
} 

有多種問題:

  • 參數是不是在所有
  • 使用沒有其他的構造這需要一個參數(即this(yourCostHere);不會編譯)
  • yourCostHere是一個未知的符號
  • getBalance違反命名規則,因爲getXxx意味着一個getter方法上面

一些一般性的建議指出:

  • 格式化你的代碼將幫助您發現錯誤
  • 使用IDE(如Eclipse或Netbeans)將幫助您發現並修復錯誤
  • 如何調試您的應用程序(再次使用IDE最簡單)將幫助您找到運行時錯誤,如上面的陰影錯誤
  • 其中最重要的一點:在發佈問題之前,請確保您的代碼是正確並且格式良好,然後只發布相關部分以及發生錯誤的必要信息(例如,標記編譯器/堆棧跟蹤指示的行)
+0

和構造函數params不正確它應該是10而11是可能應該擺脫'int getBalance'或添加餘額作爲參數 – Raghunandan

+0

@Raghunandan謝謝,我添加它 – Thomas

0

有一個非常簡單的解決方案:使用自動代碼格式化。例如在eclipse中:上下文菜單 - >源代碼 - >格式

這樣做,您會注意到setmobileServiceProvider是在類塊外部定義的,這沒有任何意義。在類塊內移動此方法及其後的任何內容,並且錯誤消失。

沒有乾淨的格式化代碼,就不可能理解這樣的錯誤。

相關問題