考虑到我不是已经在我的Weight
方法中做到了,我不理解我的编译器如何找不到类public boolean checkWeight
?编译器指向以下行:
`Weight first = new Weight();`
和
`Weight second = new 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);
}
}
答案 0 :(得分:0)
您应该像这样定义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
或使用Weight
关键字导入import
类。
答案 1 :(得分:0)
您的代码中缺少您的Weight类,请参见以下代码
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
**
答案 2 :(得分:0)
答案 3 :(得分:0)
您的公共方法可能只是:
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);
}