2013-10-22 59 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是在类块外部定义的,这没有任何意义。在类块内移动此方法及其后的任何内容,并且错误消失。

没有干净的格式化代码,就不可能理解这样的错误。

相关问题