2013-09-25 3 views
0

Lorsque j'essaie d'exécuter ce code, il renvoie la valeur erronée et je n'arrive pas à déterminer où je me suis trompé. Il imprime correctement après avoir ajouté 10 à catUp, mais quand je vérifie pour voir si cette même valeur est supérieure à 199, il tombe à travers cette déclaration si pour une raison quelconque. Aussi, quand je l'imprime à la fin de la méthode upCategory donne la valeur 1, mais quand je vais l'imprimer dans la principale, il me donne une valeur de 3.Problème de logique java dans la méthode

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); 
    } 

code supplémentaire qui va de pair avec il ...

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; 
    } 
} 

Répondre

4

Cette ligne est le problème:

if (catUp > 199.0); 

Java traite le point-virgule comme le corps de l'instruction if, et le bloc accolades en dessous du if devient un bloc normal et est toujours exécuté.

Pour fixer le bloc entre accolades à la déclaration if, retirer le point-virgule:

if (catUp > 199.0)