gpt4 book ai didi

java - 无法弄清楚我的逻辑错误Java

转载 作者:行者123 更新时间:2023-12-02 06:42:35 25 4
gpt4 key购买 nike

当我运行它时,它返回的值是当前工作站的两倍加上增量值。而不仅仅是当前站加上增量值。我无法弄清楚这是否是 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;
}

因为您已经在 up 方法中添加了 currentStation

只是建议,与问题无关:

  • 考虑“selectRadio”方法。拥有它的目的是什么。
  • 您有 Radio 作为属性,还有该类的其他三个实现。我认为你应该考虑使用“实例”运算符。

关于java - 无法弄清楚我的逻辑错误Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18994684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com