简体   繁体   English

骰子滚动模拟

[英]Dice Roll Simulation

So I am lost. 所以我迷路了。 This is the goal: 这是目标:

Ask how many times to roll one six-sided dice. 问多少次掷一个六面骰子。 Randomly roll this dice. 随机掷骰子。 Print out how many times each number between one and six appeared 打印出每个数字在1到6之间出现了多少次

Is this loop thing on the right track? 这个循环事情是否正确? What should I begin with I'm really confused sorry 我应该从什么开始我真的很困惑

while(dice > 0)
{
    rolldice = gen.nextInt(6) + 1;  //(1-6)
    if (rolldice == 1)
    {
        one++;
    }
    else if (rolldice == 2)
    {
        two++;
    }
    else if (rolldice == 3)
    {
        three++;
    }
    else if (rolldice == 4)
    {
        four++;
    }
    else if (rolldice == 5)
    {
        five++;
    }
    else if (rolldice == 6)
    {
        six++;
    }
}

Im assuming the dice integer is the input from the user. 我假设骰子整数是用户的输入。 If you decrement dice each time you iterate, you will roll the dice(go through the loop) as many times as the user asked. 如果每次迭代都减少骰子,则将骰子滚动(遍历循环),次数取决于用户要求。

Not sure if you have implemented it so that the user can input a number of dicerolls yet, if thats what you need help with take a look at Scanner. 不知道您是否已实现它,以便用户可以输入许多骰子,如果您需要帮助,请参阅“扫描器”。

Scanner in = new Scanner(System.in);
Random gen = new Random();
int rollDice;
int one = 0, two = 0, three = 0, four = 0, five = 0, six = 0;

System.out.println("How many die do you want to roll: ");

int dice = in.nextInt();

while(dice > 0)
{
    rolldice = gen.nextInt(6) + 1;  //(1-6)
    if (rolldice == 1)
    {
        one++;
    }
    else if (rolldice == 2)
    {
        two++;
    }
    else if (rolldice == 3)
    {
        three++;
    }
    else if (rolldice == 4)
    {
        four++;
    }
    else if (rolldice == 5)
    {
        five++;
    }
    else if (rolldice == 6)
    {
        six++;
    }
    dice--;
}

Now just print out the variables and you are done! 现在只需打印出变量即可完成操作!

I could also do this with a switch statement, but I'll let you figure out how to convert if thats what you want to use. 我也可以使用switch语句来做到这一点,但是我将让您弄清楚如果要使用的内容该如何进行转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM