在java中查找两个随机数之间的平均值,然后计算一个数字在switch语句中滚动的次数

在java中查找两个随机数之间的平均值,然后计算一个数字在switch语句中滚动的次数,java,random,average,Java,Random,Average,你好,我是斯塔克 需要代码帮助的新问题。到目前为止,我正在制作一个随机数生成器,它将选择一个1-100之间的随机数作为卷数,然后为1-6之间的数字滚动一个模具。然后,它打印出每一卷和卷起到1-100之间的随机数的数字 我的问题是这个。让我们简单地说,在1-100之间生成的随机数是9,每个辊的顺序是 1,6,3,5,4,2,1,6,6 输出良好,平均值良好。我遇到的新问题是: 在程序中添加一个switch语句,以跟踪每个数字出现的次数 我有一个基本的想法,如何做到这一点,但我有点困惑,如何真正去做

你好,我是斯塔克

需要代码帮助的新问题。到目前为止,我正在制作一个随机数生成器,它将选择一个1-100之间的随机数作为卷数,然后为1-6之间的数字滚动一个模具。然后,它打印出每一卷和卷起到1-100之间的随机数的数字

我的问题是这个。让我们简单地说,在1-100之间生成的随机数是9,每个辊的顺序是

1,6,3,5,4,2,1,6,6

输出良好,平均值良好。我遇到的新问题是:

在程序中添加一个switch语句,以跟踪每个数字出现的次数

我有一个基本的想法,如何做到这一点,但我有点困惑,如何真正去做这一点,并执行它。请帮忙

我当前的代码: 更新:第一个问题已经解决,新问题如上所述


再次感谢

将变量中的所有随机数相加,然后除以转鼓数

double sum = 0;
for (int i = 1; i <= rolls ; i++) {
    int dienumber = (int)(Math.random()*6+1);
    sum += dienumber;
    System.out.println(i + "\t\t" + dienumber);
}
double avg = sum / rolls;

您还可以将随机数收集到一个数组中,并使用流计算平均值:

int rolls = (int)(Math.random()*100);
int rollsArray[] = new int[rolls];
for (int i = 1; i < rollsArray.length ; i++)
{
    rollsArray[i] = (int)(Math.random()*6+1);

}
System.out.println(Arrays.stream(rollsArray).average()); 
// example output OptionalDouble[3.0]

您可以创建一个int变量,该变量将在每次滚动后计算循环内的总数。循环结束后,将这个总数除以你的转鼓数

class Main {
     public static void main(String[] args) 
    {

        int rolls = (int)(Math.random()*100);
        int total =0
        System.out.println("Number of Rolls: "+ rolls);

        System.out.println(" ");
        System.out.println("Rolls\t\tNumber");
        for (int i = 1; i <= rolls ; i++)
        {
            int dienumber = (int)(Math.random()*6+1);    
            System.out.println(i + "\t\t" + dienumber);
            total += dienumber; 
        }
        double average = total /(1.0d*rolls);
        System.out.println("Average = "+average);
    }
}

这将生成范围为[0,99]而不是[1100]的数字:

int rolls = (int)(Math.random()*100);
就性能而言,重复使用Random而不是反复调用Math.Random是个好主意:

public class Main {

    private static final Random RANDOM = new Random();

    public static void main(String[] args) {
        int rolls = RANDOM.nextInt(99) + 1;
请注意,如果您曾经使用随机性来生成安全敏感的东西,如激活码,请使用

要计算平均值,只需跟踪总和:

int sum = 0;
for (int i=1; i <= rolls ; i++) {
    int dienumber = RANDOM.nextInt(6) + 1;    
    System.out.println(i + "\t\t" + dienumber);
    sum += dieNumber;
}
double average = ((double) sum) / rolls;
System.out.printf("Average: %s%n", average);
您的代码会这样使用:

Average average = new Average();
for (int i=1; i <= rolls ; i++) {
    int dienumber = RANDOM.nextInt(6) + 1;    
    System.out.println(i + "\t\t" + dienumber);
    average.add(dieNumber);
}
System.out.printf("Average: %s%n", average.getAverage());
编辑:一般来说,当某人或你的家庭作业告诉你添加开关语句时:不要这样做。Switch语句是邪恶的黏液池,它聚集了2000名PHP开发人员的项目室中的恶魔和恶臭。不要。我可以推荐阅读

但严肃地说:记录一个数字出现的次数是一个有效的要求。告诉开发人员为此使用switch语句是不正确的。这是用户经常遇到的一种趋势:他们倾向于指定他们设想的解决方案,而不是制定他们想要解决的问题。这是危险的,因为如果你买了它,你很可能一开始就走错了路。利益相关者提出的解决方案通常不是最优的,并且植根于他们过去的领域经验,而不是技术和设计知识

相反,在这种情况下,使用地图跟踪结果。如果情况比像这里这样跟踪一个值更为多样,请返回到以面向对象的方式解决问题

由于我已经提出了可爱的“Average”类,我们可以将其重命名为“Statistics”,并用一张地图装饰它:

public class Statistics {

    private int total;
    private int count;
    private Map<Integer, Integer> distribution = new HashMap<>();

    public synchronized void add(int value) {
        total += value;
        count++;
        updateDistribution(value);
    }

    private void updateDistribution(int value) {
        Integer count = distribution.get(value);
        if (count == null) {
            count = 0;
        }
        count++;
        distribution.put(value, count);

    public double getAverage() {
        if (count == 0) {
            throw new IllegalStateException("Cannot calculate average, no values added");
        }
        return total / count;       
    }

    public double getTotal() {
        return total;
    }

    public int getCount() {
        return count;
    }

    public synchronized void reset() {
        total = 0;
        count = 0;
        distribution.clear();
    }

    public int getCount(int value) {
        Integer count = distribution.get(value); 
        if (count == null) {
            count = 0;
        }
    }
}

功能方法Java 1.8+:

{
    // ...
    double average = IntStream.range(0, rolls)
            .map(this::roll)
            .average()
            .orElse(0);
    System.out.println("Average : " + average);

}

private int roll(int i) {
    int dieNumber = (int) (Math.random() * 6 + 1);
    System.out.println(i + "\t\t" + dieNumber);
    return dieNumber;
}

将dienumber添加到另一个变量,并在末尾除以rolls。请记住,如果新变量是int,它将忽略任何浮点。例如,7/3=2。这是你的作业吗?我做了这个,现在平均每次打印3.00。@neil我得到了很好的结果。你能分享你的更新代码吗?编辑了上面的代码,因为我有一个新问题。更新的代码在上面,我使用了其他注释代码,他们的代码运行良好。@neil sum应该是双精度的,而不是int。int/int将导致int。它总是3,因为数字的范围很小。铸造总和至双平均值=双总和/转鼓;我也会这样做。
public class Statistics {

    private int total;
    private int count;
    private Map<Integer, Integer> distribution = new HashMap<>();

    public synchronized void add(int value) {
        total += value;
        count++;
        updateDistribution(value);
    }

    private void updateDistribution(int value) {
        Integer count = distribution.get(value);
        if (count == null) {
            count = 0;
        }
        count++;
        distribution.put(value, count);

    public double getAverage() {
        if (count == 0) {
            throw new IllegalStateException("Cannot calculate average, no values added");
        }
        return total / count;       
    }

    public double getTotal() {
        return total;
    }

    public int getCount() {
        return count;
    }

    public synchronized void reset() {
        total = 0;
        count = 0;
        distribution.clear();
    }

    public int getCount(int value) {
        Integer count = distribution.get(value); 
        if (count == null) {
            count = 0;
        }
    }
}
{
    // ...
    double average = IntStream.range(0, rolls)
            .map(this::roll)
            .average()
            .orElse(0);
    System.out.println("Average : " + average);

}

private int roll(int i) {
    int dieNumber = (int) (Math.random() * 6 + 1);
    System.out.println(i + "\t\t" + dieNumber);
    return dieNumber;
}