0

使用 MyProgrammingLab 练习编程并得到以下编译错误:ApartmentBuilding.java:4: error: expected

它还给了我以下提示: • 您应该使用:isLuxuryBuilding • 您确定要使用:“ • 您确定要使用:>=

这是要求:假设存在一个建筑类。定义一个子类 ApartmentBuilding,它包含以下实例变量:整数、numFloors、整数、unitsPerFloor、布尔值、hasElevator、布尔值、hasCentralAir 和字符串,managementCompany 包含管理建筑物的房地产公司的名称。有一个包含用于初始化上述变量的参数的构造函数(与上面出现的顺序相同)。还有两种方法:第一种,getTotalUnits,不接受参数,返回建筑单元总数;第二,isLuxuryBuilding 不接受任何参数,如果建筑物有中央空调、电梯和每层 2 个或更少的单元,则返回 true。

我的 SC:

public class ApartmentBuilding extends Building {
private int numFloors, unitsPerFloor;
private boolean hasElevator, hasCentralAir;
private String "managingCompany";

public ApartmentBuilding(int numFloors, int unitsPerFloor, boolean hasElevator, boolean hasCentralAir, String "managingCompany") {
this.numFloors       = numFloors;
this.unitsPerFloor   = unitsPerFloor;
this.hasElevator     = hasElevator;
this.hasCentralAir   = hasCentralAir;
this.managingCompany = managingCompany;
}

public int getTotalUnits() {return unitsPerFloor * numFloors;}
public boolean isLuxuyBuilding() {if(unitsPerFloor <= 2 && hasElevator >= 2 && hasCentralAir >= 2) {return true;}
else {System.err.println(managingCompany + " is not luxury");}}}
4

1 回答 1

1

您不能在变量名中使用引号

改变

private String "managingCompany";

private String managingCompany;

也不能与此语句中的数字整数进行比较hasElevatorboolean

if (unitsPerFloor <= 2 && hasElevator >= 2 && hasCentralAir >= 2) {

该方法isLuxuyBuilding必须返回一个boolean. 该else语句没有任何返回值。

你可以做:

public boolean isLuxuyBuilding() {
   if (unitsPerFloor <= 2 && hasElevator && hasCentralAir) {
      return true;
    } else {
      return false;
    }
}

请参阅:变量

于 2012-10-24T23:13:14.350 回答