我需要考虑模具的类型并实现可测量的接口。
然后,我必须生成骰子,将其投射并添加到DataSet类的第一个实现中,然后显示平均值。我的问题是求平均值。当我尝试将代码放入测试器时,将其从DataSet类中拉出,但是会收到错误消息。 VS Code强调了“ ds.add(d);” ,当我这样做时,推荐的修复程序会弄乱很多东西。它将可测量的工具添加到整个类中,然后添加{{1 }} 方法。它仅返回平均值0,我认为这是因为getMeasure()
方法设置为返回0,而我不知道如何解决它。请帮忙。
谢谢!
模具课程和测试人员
getMeasure()
数据集
import java.util.Random;
public class P11_1 implements Measurable
{
private Random generator;
private int sides;
public P11_1(int s)
{
sides = s;
generator = new Random();
}
public int cast()
{
return 1 + generator.nextInt(sides);
}
public double getMeasure() {
return 0;
}
public static void main(String[] args)
{
final int TRIES = 10;
DataSet ds = new DataSet();
P11_1 d = new P11_1(6);
for(int i = 1; i <= TRIES; i++)
{
int n = d.cast();
ds.add(d);
System.out.print(n + " ");
}
System.out.println("Average: " + ds.getAverage());
}
}
可衡量的
public class DataSet
{
private double sum;
private int count;
private Measurable maximum;
public DataSet()
{
sum = 0;
count = 0;
maximum = null;
}
public void add(Measurable d) {
sum = sum + d.getMeasure();
if(count == 0 || maximum.getMeasure() < d.getMeasure())
{
maximum = d;
}
count++;
}
public Measurable getMaximum()
{
return maximum;
}
public double getAverage()
{
if(count == 0) return 0;
else return sum / count;
}
}