简体   繁体   English

方法中的Java逻辑问题

[英]java logic issue in method

When i attempt to run this code it keeps returning the wrong value and i can not figure out where i went wrong. 当我尝试运行此代码时,它将不断返回错误的值,并且我无法弄清楚哪里出了问题。 It prints out correctly after I add 10 to catUp but when I check to see if that same value is greater then 199 it falls through that if statement for some reason. 我将10添加到catUp后,它可以正确打印,但是当我检查该值是否大于199时,由于某种原因它会通过该if语句。 Also when I print it out at the end of the upCategory method it gives the value 1, but when I go to print it out in the main it gives me a value of 3. 同样,当我在upCategory方法的末尾将其打印出来时,它的值为1,但是当我在主窗口中打印时,它的值为3。

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

Additional code that goes with it... 附带的其他代码...

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

This line is the problem: 这行是问题所在:

if (catUp > 199.0);

Java treats the semicolon as the body for the if statement, and the block in braces below the if becomes a normal block, and is always executed. Java的把分号作为机构的if语句,然后在下面的括号中的块if成为一个正常的块,并始终执行。

To attach the block in braces to the if statement, remove the semicolon: 要将大括号中的块附加到if语句,请删除分号:

if (catUp > 199.0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM