3 回答

TA貢獻(xiàn)1783條經(jīng)驗 獲得超4個贊
方法參數(shù)“maxWeight”的類型為“Weight”,Java無法找到解析程序依賴關(guān)系的定義。未能通過類路徑找到類將導(dǎo)致錯誤“找不到符號”。您可能希望定義類“Weight”并將其導(dǎo)入,就像其他人對您所做的那樣。
你真的需要“重量”類嗎?您只是將通過的論點與“警衛(wèi)”的stashWeight進(jìn)行比較。
您的公共方法可以簡單地是:
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貢獻(xiàn)1802條經(jīng)驗 獲得超10個贊
您的代碼中缺少權(quán)重類 請參閱以下代碼
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貢獻(xiàn)1875條經(jīng)驗 獲得超3個贊
你應(yīng)該像這樣定義類: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
或者使用關(guān)鍵字導(dǎo)入您的類。Weightimport
添加回答
舉報