2013-09-25 75 views
0

當我嘗試運行此代碼時,它一直返回錯誤的值,我無法弄清楚我出錯的地方。它在我向catUp添加10後正確輸出,但是當我檢查相同的值是否大於199時,它由於某種原因而落入if語句。此外,當我把它打印出來,在它提供的價值1 upCategory方法結束,但是當我去打印出來在主它給我的3方法中的java邏輯問題

public void upCategory() 
    { 
    double catUp = radioXM.getCurrentStaion(); 
    catUp += 10; 
    System.out.println(catUp); 
    if (catUp > 199.0); 
    { 
     catUp = 1; 
     radioXM.setCurrentStation(catUp); 
     System.out.println(catUp); 
    } 
    radioXM.setCurrentStation(catUp); 
    System.out.println(catUp); 
    } 
public static void main (String [] args) { 
    AutoRadioSystem c = new AutoRadioSystem(); 
    c.selectRadio(); 
    double b = c.getCurrentStation(); 
    System.out.println(b); 

    // this changes the radio to XM 
    c.selectRadio(); 
    double d = c.getCurrentStation(); 
    System.out.println(d); 

    //this is suppose to change the station up by 10 but gives incorrect value 
    c.upCategory(); 
    double f = c.getCurrentStation(); 
    System.out.println(f); 
    } 

與去其他代碼的值它...

public abstract class Radio 
{ 
double currentStation; 

RadioSelectionBar radioSelectionBar; 
public Radio() 
{ 
    this.currentStation = getMin_Station(); 
} 
public abstract double getMax_Station(); 
public abstract double getMin_Station(); 
public abstract double getIncrement(); 
public void up() 
{ 

} 
public void down() 
{ 

} 
public double getCurrentStaion() 
{ 
    return this.currentStation; 
} 
public void setCurrentStation(double freq) 
{ 
    currentStation += freq; 
} 
public void setStation(int buttonNumber, double station) 
{ 
} 
public double getStation(int buttonNumber) 
{ 
    return 0.0; 
} 
public String toString() 
{ 
    String message = ("" + currentStation); 
    return message; 
} 
    public boolean equals (Object o) 
    { 
    if (o == null) 
     return false; 
    if (! (o instanceof Radio)) 
     return false; 
    Radio other = (Radio) o; 
    return this.currentStation == other.currentStation; 
    } 

public class XMRadio extends Radio 
{ 
    private static final double Max_Station = 199; 
    private static final double Min_Station = 1; 
    private static final double Increment = 1; 
    public XMRadio() 
    { 
    } 
    public double getMax_Station() 
    { 
    return this.Max_Station; 
    } 
    public double getMin_Station() 
    { 
    return this.Min_Station; 
    } 
    public double getIncrement() 
    { 
    return this.Increment; 
    } 
    public String toString() 
    { 
    String message = ("XM "+ currentStation); 
    return message; 
    } 
} 

回答

4

這條線的問題是:

if (catUp > 199.0); 

的Java把分號作爲身體爲if聲明,並在下面括號中塊成爲普通塊,並且始終執行。

要在大括號附加塊到if語句,移除分號:

if (catUp > 199.0)