逻辑错误可能是java赋值中的误解

时间:2013-09-24 18:12:43

标签: java logic

我一直遇到很多问题让这个项目正常工作但我目前仍然坚持让这个课程正常工作。它假设要做的是从无线电课中取出当前电台并将其传递给这个班级。问题是我试图在AM和FM之间进行选择,但每次运行它时,它只显示AM站。我不明白为什么它会自动设置到该站。

public class AutoRadioSystem
{
  private Radio selectedRadio;
  private AMRadio radioAM;
  private FMRadio radioFM; 
  private XMRadio radioXM;

  //is this the correct place to initialize these? 
  Radio amRadio = new AMRadio();
  Radio fmRadio = new FMRadio();
  public AutoRadioSystem()
  {
   //even making the selected radio FM still produces values for AM
   selectedRadio = radioFM;
  }

  // this is where my problem currently lies and probably much more. Shouldn't it return 0.0 without any station being selected.  
  public double getCurrentStation()
  {
    if (selectedRadio == radioAM)
    {
      return amRadio.getCurrentStaion();
    }
    else if (selectedRadio == radioFM)
    {
      return fmRadio.getCurrentStaion();
    }
    return 0.0;
  }

  //I'm not sure if i'm setting this up correctly to switch the radio from am to fm 
  public void selectRadio()
  {
    if (selectedRadio == radioAM)
      selectedRadio = radioFM;
  }
  public static void main (String [] args) { 
    AutoRadioSystem c = new AutoRadioSystem();
    c.selectRadio();
    double b = c.getCurrentStation();
    System.out.println(b);
  }
}

public class AMRadio extends Radio
{
  private static final double Max_Station = 1605;
  private static final double Min_Station = 535;
  private static final double Increment = 10;
  public AMRadio()
  {
    currentStation = Min_Station;
  }
  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 = ("AM " + this.currentStation);
    return message;
  } 
}

public class FMRadio extends Radio
{
  private static final double Max_Station = 108.0;
  private static final double Min_Station = 88.0;
  private static final double Increment = .01;
  public FMRadio()
  {
    currentStation = Min_Station;
  }
  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 = ("FM " + this.currentStation);
    return message;
  } 
}


public abstract class Radio
{
 double currentStation;

 RadioSelectionBar radioSelectionBar;
 public Radio()
 {

 }
 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)
 {
   this.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;
  } 
 }

3 个答案:

答案 0 :(得分:1)

问题是,在.getCurrentStation()中,selectedRadio& radioAM不是init而且为null。

错误始于:

public void selectRadio()
{
    if (selectedRadio == radioAM)
    {
        selectedRadio = radioFM;
    }
}

这里,selectedRadio = null,所以永远不会得到赋值。

编辑:我相信你只是从这开始,所以更多细节会有所帮助。

  1. 宣布两个字段amRadio& radioAM然后初始化其中一个并使用另一个。
  2. 您没有将值设置为selectedRadio并进行比较,这总是返回false
  3. 实例初始值的最佳位置是构造函数方法,这里是AutoRadioSystem()
  4. 您可能希望将代码更改为:

    private Radio selectedRadio;
    public AutoRadioSystem()
    {
        selectedRadio = new FMRadio();
    }
    // To compare, using instanceOf, but better design will use enum value instead, up to you
    

答案 1 :(得分:1)

我想我已经找到了问题

每个Radio重载都有2个字段

private AMRadio radioAM;
...
Radio amRadio = new AMRadio();

但是你要比较的那个:radioAM永远不会被实例化,因此总是为空。


致电时

if (selectedRadio == radioAM)

selectedRadioradioAM都是null,所以他们当然是平等的


除非您打算将radioAMamRadio视为完全不同的实例,否则您不应该有2个这样的字段。

由于您正在使用多态,因此您可能希望使用后者

Radio amRadio = new AMRadio();

答案 2 :(得分:0)

所有属性selectedRadio,radioAM和RadioFM均为空。构造函数中的代码无效,因为selectedRadio = RadioFM。这意味着selectedRadio的值不会改变并保持为零。

因此,getCurrentStation中的selectedRadio == radioAM(null == null)始终为true。这将始终在方法getCurrentStation中应用第一个if-block,并始终返回“amradio”。

卡欧