2014-11-21 31 views
0

我的代碼中似乎有一個邏輯錯誤。這是因爲我的第一個風扇對象(FanOne)應該顯示以下輸出:速度:2,半徑:10.0,顏色:黃色。我的代碼內部的邏輯錯誤

相反,它已顯示出速度:1

我會認爲這是一個錯誤的東西與我的setSpeed()方法..但對我來說,好像一切都沒有問題了預期效果。請指教,謝謝。

public class TestFan { 
public static void main(String[] args) { 

Fan FanOne = new Fan(); 

FanOne.fanOn(); 
FanOne.setColor("yellow"); 
FanOne.setCustomSpeed("MEDIUM"); 
FanOne.setCustomRadius(10); 

System.out.println(FanOne.toString()); 

System.out.println(); 

Fan FanTwo = new Fan(); 

FanTwo.fanOff(); 
FanTwo.setCustomRadius(5); 
FanTwo.setColor("blue"); 

System.out.println(FanTwo.toString()); 

} 
} 

public class Fan { 
// Declare constant data fields 
final int SLOW = 1; 
final int MEDIUM = 2; 
final int FAST = 3; 

private int speed; 
private boolean on; 
private double radius; 
private String color; 

// Construct a default fan 
public Fan() { 
    speed = SLOW; 
    on = false; 
    radius = 5; 
    color = new String("Blue"); 
} 

// Set fan off 
public boolean fanOff() { 
    on = false; 
    return on; 
} 

// Set fan on 
public boolean fanOn() { 
    on = true; 
    return on; 
} 

public double getRadius() { 
    return radius; 
} 

// Set custom radius 
public void setCustomRadius(double rad) { 
    radius = rad; 
} 

public int getSpeed() { 
    return speed; 
} 

// Set custom speed 
public String setCustomSpeed(String speed) { 
    if (speed.equals("SLOW")) { 
    this.speed = SLOW; 
} else if (speed.equals("MEDIUM")) { 
    this.speed = MEDIUM; 
} else if (speed.equals("FAST")) { 
    this.speed = FAST; 
} 
return speed; 
} 

public String getColor() { 
return color; 
} 

public void setColor(String colorName) { 
color = colorName; 
} 

public String toString() { 
if (on == true) { 
    return ("Speed: " + speed + ", " + "Radius: " + radius + ", " + "Color: " + color); 
} else { 
    return ("Color: " + color + ", " + "Radius: " + radius + ", Alert: " + "The fan is off!"); 
} 

} 

} 
+0

是Daniel Liang介紹Java教科書的作業。我意識到這個問題已經使用他的書教了很多年! – hfontanez 2014-11-21 20:58:34

回答

2

在你的setter方法爲speedsetCustomSpeed,你只能改變局部變量Stringspeed,而不是實例變量speed。它保持不變,在SLOW1

使用this來限定您指的是實例變量,例如,

this.speed = SLOW; 

這是一個int所以沒有必要你不斷轉換爲String這裏。您可以相應地更改其他賦值語句,也可以返回this.speed

+0

賓果!!!!!!!!!! – hfontanez 2014-11-21 21:00:15

+0

我改變了我的代碼以反映您討論的範例。唯一的是我一直在「返回this.speed」上發生錯誤。我改變它返回產生正確輸出的速度。我不完全確定這是爲什麼起作用。雖然我會在def變量範圍和shadowing上進行讀取;) – Chewy 2014-11-21 22:17:00

+0

你有的返回類型是當前的'String','speed'是'String',所以工作。您也可以返回'this.speed',並將方法的返回類型更改爲'int',並且根據您希望返回的內容 - 新的存儲值或原始的'String'輸入,這也可以工作。 – rgettman 2014-11-21 22:18:23

2

你的問題叫做陰影。您的代碼在這裏:

// Set custom speed 
public int setCustomSpeed(String speed) { 
    if (speed.equals("SLOW")) { 
    speed = String.valueOf(SLOW); 
} else if (speed.equals("MEDIUM")) { 
    speed = String.valueOf(MEDIUM); 
} else if (speed.equals("FAST")) { 
    speed = String.valueOf(FAST); 
} 
return speed; 
} 

是設置局部變量的速度等於2,離開風扇級變速等於不管它是什麼之前(構造函數將其設置爲1)。因此,我們需要使用this.speed來區分對象的速度變量。這表明使用哪個變量。

// Set custom speed 
public String setCustomSpeed(String speed) { 
    if (speed.equals("SLOW")) { 
    this.speed = String.valueOf(SLOW); 
} else if (speed.equals("MEDIUM")) { 
    this.speed = String.valueOf(MEDIUM); 
} else if (speed.equals("FAST")) { 
    this.speed = String.valueOf(FAST); 
} 
return this.speed; 
} 

爲了避免這種情況在未來,你應該考慮使用不同的命名變量,並閱讀並理解變量的作用域和陰影。

+0

如果我沒有記錯,常量字段名稱是在賦值中定義的;這不是OP的選擇。 – hfontanez 2014-11-21 21:03:49

+1

@hfontanez是的,但是OP不會一直在做家庭作業,並且最終會自行開發代碼,在這種情況下,知道 – 2014-11-21 21:05:48

+1

的確是這樣:......作爲一個建議,我會使用'equalsIgnoreCase '而不是'equals' – hfontanez 2014-11-21 21:09:40