3 回答
TA贡献1783条经验 获得超5个赞
方法参数“maxWeight”的类型为“Weight”,Java无法找到解析程序依赖关系的定义。未能通过类路径找到类将导致错误“找不到符号”。您可能希望定义类“Weight”并将其导入,就像其他人对您所做的那样。
你真的需要“重量”类吗?您只是将通过的论点与“警卫”的stashWeight进行比较。
您的公共方法可以简单地是:
public boolean checkWeight(int maxWeight) {
return maxWeight >= stashWeight;
}
主方法可以更新为:
public static void main(String[] args) {
int firstWeight = 3000;
int secondWeight = 120;
Guard grams = new Guard(450);
System.out.println("Officer, can I come in?");
boolean canFirstManGo = grams.checkWeight(firstWeight);
System.out.println(canFirstManGo);
System.out.println();
System.out.println("Officer, how about me?");
boolean canSecondManGo = grams.checkWeight(secondWeight);
System.out.println(canSecondManGo);
}
TA贡献1802条经验 获得超10个赞
您的代码中缺少权重类 请参阅以下代码
class Weight {
public int weight;
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
}
public class Guard {
int stashWeight;
public Guard(int maxWeight) {
this.stashWeight = maxWeight;
}
public boolean checkWeight(Weight maxWeight) {
if (maxWeight.weight >= stashWeight) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
Weight first = new Weight();
first.weight = 3000;
Weight second = new Weight();
second.weight = 120;
Guard grams = new Guard(450);
System.out.println("Officer, can I come in?");
boolean canFirstManGo = grams.checkWeight(first);
System.out.println(canFirstManGo);
System.out.println();
System.out.println("Officer, how about me?");
boolean canSecondManGo = grams.checkWeight(second);
System.out.println(canSecondManGo);
}
}
输出
**
Officer, can I come in?
true
Officer, how about me?
false
**
TA贡献1875条经验 获得超3个赞
你应该像这样定义类:Weight
public class Weight {
public int weight;
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
}
输出:
Officer, can I come in?
true
Officer, how about me?
false
或者使用关键字导入您的类。Weightimport
添加回答
举报
