循环中的随机数和计数次数

时间:2014-10-03 01:33:26

标签: java loops

我是Java的新手,目前有一个处理循环和随机数的赋值。问题如下:修改ThrowingPairsOfDice.java进行计数,并将该对总和为2,总和为3,总和为4,总和为5,总和为6.显示结果。

这是需要修改的代码:

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

public class ThrowingPairsOfDice
{

/**
 * main method simulates throwing 2 dice 1,000,000 times
 * 
 */
    public static void main(String[] args)
    {
        Random die1 = new Random();
        Random die2 = new Random();
        int throwOfDice=0;

        System.out.println("How many times should I throw two dice?");
        Scanner kb = new Scanner(System.in);

        int numberOfTimes = kb.nextInt();

        // throw the dice the number of times requested

        for (int i=1;i<=numberOfTimes;i++)
        {
            int toss1 =die1.nextInt(5)+1;
            int toss2 =die2.nextInt(5)+1;
            throwOfDice = toss1 + toss2;
            System.out.println(toss1+ "+" +toss2+ "= " +throwOfDice);
        }

    }
}

我不知道如何修改这个程序来计算这对骰子总和到给定值的次数,所以非常感谢帮助!感谢。

2 个答案:

答案 0 :(得分:1)

您需要使用额外的变量来存储#totalals。在我的回答中,我使用数组sum []并使用模具减去2的总和来引用它。

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

public class ThrowingPairsOfDice
{

/**
 * main method simulates throwing 2 dice 1,000,000 times
 * 
 */
    public static void main(String[] args)
    {
        Random die1 = new Random();
        Random die2 = new Random();
        int throwOfDice=0;
        int [] sum = {0, 0, 0, 0, 0};


        System.out.println("How many times should I throw two dice?");
        Scanner kb = new Scanner(System.in);

        int numberOfTimes = kb.nextInt();

        // throw the dice the number of times requested

        for (int i=1;i<=numberOfTimes;i++)
        {
            int toss1 =die1.nextInt(5)+1;
            int toss2 =die2.nextInt(5)+1;
            throwOfDice = toss1 + toss2;
            System.out.println(toss1+ "+" +toss2+ "= " +throwOfDice);
            if (throwOfDice < 7) sum[throwOfDice - 2]++;
        }

        for (int i=0;i<=4;i++)
        {
            System.out.println("# of throws totalling " + (i + 2) + " = " + sum[i]);
        }
    }
}

答案 1 :(得分:0)

您只有新的Random个实例可生成两个随机int值,您可以使用printf()获取格式化输出,并使用ifelse if求和你的骰子。像,

Random die = new Random(); // <-- only need one.
// The number of sides to the die.
int sides = 6;
// Variables to count the results
int rolled2 = 0;
int rolled3 = 0;
int rolled4 = 0;
int rolled5 = 0;
int rolled6 = 0;

// throw the dice the number of times requested
for (int i = 1; i <= numberOfTimes; i++) {
    int toss1 = 1 + die.nextInt(sides);
    int toss2 = 1 + die.nextInt(sides);
    int throwOfDice = toss1 + toss2;
    System.out.printf("%d + %d = %d%n", toss1, toss2, throwOfDice);
    if (throwOfDice == 2) {
        rolled2++;
    } else if (throwOfDice == 3) {
        rolled3++;
    } else if (throwOfDice == 4) {
        rolled4++;
    } else if (throwOfDice == 5) {
        rolled5++;
    } else if (throwOfDice == 6) {
        rolled6++;
    }
}
System.out.printf("2 = %d, 3 = %d, 4 = %d, 5 = %d, 6 = %d%n", rolled2,
        rolled3, rolled4, rolled5, rolled6);