-5

您好,我收到错误消息:预期类接口或枚举,这是什么意思?

我的代码:

/**
 * 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();
}  

}

任何答案或回复和帮助将不胜感激,因为我无法像以前那样编译它而没有语法错误。

4

2 回答 2

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未知以及缺少返回语句。这里缺少一个简单的空间。

另一个问题是您的构造函数(如 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 方法

上面提到的一些一般性建议:

  • 格式化代码将帮助您发现错误
  • 使用 Eclipse 或 Netbeans 等 IDE 将帮助您发现和修复错误
  • 学习如何调试您的应用程序(同样最容易使用 IDE)将帮助您找到运行时错误,例如上面的阴影错误
  • 最重要的一点:在您发布问题之前,请确保您的代码尽可能正确且格式正确,然后仅发布相关部分以及发生的错误的必要信息(例如标记编译器的行/stacktrace 表示)
于 2013-10-22T17:17:47.827 回答
0

有一个非常简单的解决方案:使用自动代码格式化。例如在eclipse中:Contextmenue->Source->Format

这样做,您会注意到setmobileServiceProvider在类块之外定义,这是没有意义的。在类块中移动这个方法和任何后续,错误就消失了。

如果没有格式清晰的代码,就不可能理解这些错误。

于 2013-10-22T17:23:12.933 回答