1 回答

TA貢獻1829條經(jīng)驗 獲得超13個贊
您在 CompareTo 中缺少返回語句。我在下面評論了您的原件以及更正的版本。在比較 [2,1] 和 [1,2] 的情況下,x 值不匹配,但是當(dāng)您點擊 this.x.CompareTo 時,您實際上從未返回該比較,因此您的值比較返回。
你有:
public int CompareTo(Point that)
{
if(this.val == that.val) {
if(this.x == that.x) {
return this.y.CompareTo(that.y);
}
else {
//****MISSING RETURN STATEMENT -
//will return the val.ComapreTo statement after
//it leaves this block*****
this.x.CompareTo(that.x);
}
}
return val.CompareTo(that.val);
}
你需要:
public int CompareTo(Point that)
{
if(this.val == that.val) {
if(this.x == that.x) {
return this.y.CompareTo(that.y);
}
else {
return this.x.CompareTo(that.x);
}
}
return val.CompareTo(that.val);
}
- 1 回答
- 0 關(guān)注
- 120 瀏覽
添加回答
舉報