当我运行此值时,它返回的值是currentStation的两倍加上增量值。不仅仅是currentStation加上增量值。我不知道它是否是AutoRadioSystem或Radio类中的问题。
public class AutoRadioSystem
{
private Radio selectedRadio;
private AMRadio radioAM = new AMRadio();
private FMRadio radioFM = new FMRadio();
private XMRadio radioXM = new XMRadio();
public AutoRadioSystem()
{
selectedRadio = new AMRadio();
}
public double getCurrentStation()
{
if (selectedRadio.equals(radioAM))
{
return radioAM.getCurrentStaion();
}
else if (selectedRadio.equals(radioFM))
{
return radioFM.getCurrentStaion();
}
else if (selectedRadio.equals(radioXM))
{
return radioXM.getCurrentStaion();
}
return 0.0;
}
public void selectRadio()
{
if (selectedRadio.equals(radioAM))
selectedRadio = radioFM;
else if (selectedRadio.equals(radioFM))
selectedRadio = radioXM;
else if (selectedRadio.equals(radioXM))
selectedRadio = radioAM;
}
public void upCategory()
{
double catUp = radioXM.getCurrentStaion();
catUp += 10;
if (catUp > 199.0)
{
catUp = 1;
radioXM.setCurrentStation(catUp);
}
radioXM.setCurrentStation(catUp);
}
public void up()
{
if (selectedRadio.equals(radioAM))
{
double stationUp = radioAM.getCurrentStaion();
stationUp += radioAM.getIncrement();
radioAM.setCurrentStation(stationUp);
// System.out.println(stationUp );
}
}
public boolean equals (Object o)
{
if (o == null)
return false;
if (! (o instanceof AutoRadioSystem))
return false;
AutoRadioSystem other = (AutoRadioSystem) o;
return this.selectedRadio == other.selectedRadio;
}
public static void main (String [] args) {
AutoRadioSystem c = new AutoRadioSystem();
c.selectRadio();
double b = c.getCurrentStation();
System.out.println(b);
c.selectRadio();
double d = c.getCurrentStation();
System.out.println(d);
c.upCategory();
double f = c.getCurrentStation();
System.out.println(f);
c.selectRadio();
double e = c.getCurrentStation();
System.out.println(e);
c.up();
double g = c.getCurrentStation();
System.out.println(g);
c.up();
double t = c.getCurrentStation();
System.out.println(t);
}
}
附加代码
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()
{
}
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 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 void setCurrentStation(double freq)
{
// This is equal to currentStation = currentStation + freq
currentStation += freq;
}
它应该是
public void setCurrentStation(double freq)
{
currentStation = freq;
}
因为您已经在
currentStation
方法中添加了up
。只是建议,与问题无关:
考虑一下“ selectRadio”方法。拥有它的目的是什么。
您具有
Radio
作为属性,并且还具有该类的其他三个实现。我认为您应该考虑使用'instance of'运算符。关于java - 无法找出我的逻辑错误Java,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18994684/