骰子统计方案 [英] Dice Statistics Program

查看:204
本文介绍了骰子统计方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写有要求用户的骰子滚动的数量和时间,以掷骰子量输入的Java程序。

这是我写的,到目前为止没有正确掷骰子的程序:如果我摇骰子2,然后2和24应该有卷最少的,中间的数字将有最大量辊。现在它均匀地卷每个数字pretty。

有谁知道我怎么能解决这个问题?

 导入了java.util.Random;
进口java.util.Scanner中;公共类DiceStats {
    公共静态随机兰特=新的随机();    公共静态INT rollDice(){
        诠释骰子;
        骰子=(rand.nextInt(6)+1);
        返回骰子; //返回总和掷骰
    }    公共静态无效的主要(字串[] args){
        扫描仪输入=新的扫描仪(System.in);        System.out.print(有多少骰子会构成一个滚?);
        INT diceNumber = input.nextInt();        System.out.print(有多少卷?);
        INT rollNumber = input.nextInt();        INT []频率=新INT [6 * diceNumber + 1];        的for(int i = 0; I< rollNumber;我++){
            ++频率[(rollDice()* diceNumber)];
        }        System.out.printf(%s%10S的\\ n,面子,频率);        对于(INT FACE = diceNumber;面部及LT; frequency.length;面对++)
            System.out.printf(%4D%10D \\ N,脸,频率[脸]);
    }
}


解决方案

您的问题(至少一个在这个问题)是正确的行

  ++频率[(rollDice()* diceNumber)];

而不是滚动骰子ň和求和它们,你滚动1模具和用n乘以它的。
你会希望有类似

  INT总和= 0;
的for(int i = 0; I< diceNumber;我++)
    总和+ = rollDice();
++频率[总和]

这会更准确地模拟骰子,你有单独的掷骰每个死亡。

I'm trying to write a java program that asks the user to enter in the amount of dice to roll and the amount of times to roll the dice.

The program that I wrote so far isn't rolling the dice properly: if I roll 2 dice, then 2 and 24 should have the least amount of rolls and the middle numbers would have the most amount of rolls. Right now it rolls each number pretty evenly.

Does anyone know how I can fix this?

import java.util.Random;
import java.util.Scanner;

public class DiceStats {
    public static Random rand= new Random();

    public static int rollDice() {
        int dice;
        dice=(rand.nextInt(6)+1);   
        return dice;               // returns the sum dice rolls
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("How many dice will constitute one roll? ");
        int diceNumber = input.nextInt();

        System.out.print("How many rolls? ");
        int rollNumber = input.nextInt();

        int[] frequency = new int[6*diceNumber+1];

        for (int i = 0; i < rollNumber; i++) {
            ++frequency[(rollDice()*diceNumber)];
        }

        System.out.printf("%s%10s\n", "Face", "Frequency");

        for (int face= diceNumber; face <frequency.length; face++)
            System.out.printf("%4d%10d\n", face, frequency[face]);
    }
}

解决方案

Your problem (at least the one in the question) is right in the line

++frequency[(rollDice()*diceNumber)];

Instead of rolling n dice and summing them, you're rolling 1 die and multiplying it by n. You would want to have something like

int sum = 0;
for(int i = 0;i<diceNumber;i++)
    sum+=rollDice();
++frequency[sum];

That will more accurately simulate dice, as you have individual dice rolls for each die.

这篇关于骰子统计方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆