骰子滚动直方图与循环

时间:2016-11-30 20:53:15

标签: java loops random

我班上遇到了一个我无法理解的问题。

这就是问题:

这个测验的目的是加强对循环和计数的理解,以及回顾随机数的使用。

修改下面的程序以打印直方图,其中通过打印#次等字符来显示骰子滚动等于每个可能值的总次数。每卷都会使用两个骰子。

示例:

直方图显示每个可能值的骰子总数。

骰子滚动统计(结果各不相同):

2s:######

3s:####

4s:###

5s:########

6s:###################

7s:#############

8s:#############

9s:##############

10s:###########

11s:#####

12s:####

~~~~~~~~~~~~~~~~~~~~~

我无法让程序在上面的例子中打印直方图。

这就是我到目前为止所做的:

    import java.util.Scanner;

    import java.util.Random;

    public class DiceStats {

       public static void main(String[] args) {

          Scanner scnr = new Scanner(System.in);

          Random randGen = new Random();

          int seedVal = 11;

          randGen.setSeed(seedVal);

          // FIXME 1 and 2: Set the seed to the Random number generator


          int i = 0;          // Loop counter iterates numRolls times
          int numRolls = 0;   // User defined number of rolls 


          // FIXME 3: Declare and initiate cariables needed

          int numOnes = 0;
          int numTwos = 0;
          int numThrees = 0;
          int numFours = 0;
          int numFives = 0;
          int numSixes = 0;   // Tracks number of 6s found
          int numSevens = 0;  // Tracks number of 7s found
          int numEights = 0;
          int numNines = 0;
          int numTens = 0;
          int numElevens = 0;
          int numTwelves = 0;
          int die1 = 0;       // Dice 1 values
          int die2 = 0;       // Dice 2 values
          int rollTotal = 0;  // Sum of dice values

          System.out.println("Enter number of rolls: ");
          numRolls = scnr.nextInt();

          if (numRolls >= 1) {
             // Roll dice numRoll times
             for (i = 0; i < numRolls; ++i) {
                die1 = randGen.nextInt(6) + 1;
                die2 = randGen.nextInt(6) + 1;
                rollTotal = die1 + die2;

                // FIXME 4: Count number of sixs and sevens; complete the same for all other possible values
                if (rollTotal == 1) {
                   numOnes = numOnes + 1;
                }
                if (rollTotal == 2) {
                   numTwos = numTwos + 1;
                }
                if (rollTotal == 3) {
                   numThrees = numThrees + 1;
                }
                if (rollTotal == 4) {
                   numFours = numFours + 1;
                }
                if (rollTotal == 5) {
                   numFives = numFives + 1;
                }
                if (rollTotal == 6) {
                   numSixes = numSixes + 1;
                }
                if (rollTotal == 7) {
                   numSevens = numSevens + 1;
                }
                if (rollTotal == 8) {
                   numEights = numEights + 1;
                }
                if (rollTotal == 9) {
                   numNines = numNines + 1;
                }
                if (rollTotal == 10) {
                   numTens = numTens + 1;
                }
                if (rollTotal == 11) {
                   numElevens = numElevens + 1;
                }
                else if (rollTotal == 12) {
                   numTwelves = numTwelves + 1;
                }
                System.out.println("Debugging: Roll " + (i+1) + " is " + rollTotal + " (" + die1 + 
              "+" + die2 + ")");
            }

             // Print statistics on dice rolls
             System.out.println("\nDice roll statistics:");

             // FIXME 5: Complete printing the histogram
             System.out.println("1s: " + numOnes);
             System.out.println("2s: " + numTwos);
             System.out.println("3s: " + numThrees);
             System.out.println("4s: " + numFours);
             System.out.println("5s: " + numFives);
             System.out.println("6s: " + numSixes);
             System.out.println("7s: " + numSevens);
             System.out.println("8s: " + numEights);
             System.out.println("9s: " + numNines);
             System.out.println("10s: " + numTens);
             System.out.println("11s: " + numElevens);
             System.out.println("12s: " + numTwelves);
          }
          else {
             System.out.println("Invalid rolls. Try again.");
          }

         return;
       }
    }

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:0)

有一个这样的循环,你有打印陈述。

修改你的代码,以便每次都将新变量放在一个数组中,以便你可以循环遍历它们。

 import java.util.Scanner;

import java.util.Random;

public class DiceStats {

   public static void main(String[] args) {

      Scanner scnr = new Scanner(System.in);

      Random randGen = new Random();

      int seedVal = 11;

      randGen.setSeed(seedVal);

      // FIXME 1 and 2: Set the seed to the Random number generator


      int i = 0;          // Loop counter iterates numRolls times
      int numRolls = 0;   // User defined number of rolls 


      // FIXME 3: Declare and initiate cariables needed

      int[] numValues=new int[12];
      int die1 = 0;       // Dice 1 values
      int die2 = 0;       // Dice 2 values
      int rollTotal = 0;  // Sum of dice values

      System.out.println("Enter number of rolls: ");
      numRolls = scnr.nextInt();

      if (numRolls >= 1) {
         // Roll dice numRoll times
         for (i = 0; i < numRolls; ++i) {
            die1 = randGen.nextInt(6) + 1;
            die2 = randGen.nextInt(6) + 1;
            rollTotal = die1 + die2;

            // FIXME 4: Count number of sixs and sevens; complete the same for all other possible values
           numValues[rollTotal]++;
            System.out.println("Debugging: Roll " + (i+1) + " is " + rollTotal + " (" + die1 + 
          "+" + die2 + ")");
        }

         // Print statistics on dice rolls
         System.out.println("\nDice roll statistics:");

         // FIXME 5: Complete printing the histogram
        for(int i=2;i<=12;i++)
        {
           System.out.print(i+"s: ");
           for(int j=0;j<numVales[i];j++)
           {
               System.out.print("#");
           }
           System.out.println();
      }
      else {
         System.out.println("Invalid rolls. Try again.");
      }

     return;
   }
}

如果您需要澄清问题,请告诉我。

答案 1 :(得分:0)

您可以这样做:

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);
    //You can directly set the seed during the object creation.
    Random random = new Random(System.currentTimeMillis());
    // This array is used to keep the value of your dice (2 - 12)
    int [] histogram = new int[13];

    while(true) {
        System.out.println("Enter number of rolls: ");
         int numberOfRolls = scanner.nextInt();

         //If you enter 0, you can simply terminate the program
         if(numberOfRolls == 0) break;

         for(int i = 0; i < numberOfRolls; i++) {
             int rolledValue = (random.nextInt(6) + 1) + (random.nextInt(6) + 1);
             histogram[rolledValue]++;
         }

         //Print the result to your console. 
         for(int i = 2; i < histogram.length; i++) {
            System.out.print("Total: " + i + " ");
            for(int j = 0; j <histogram[i]; j++) {
                System.out.print("#");
            }
            System.out.println();
         } 
    }
}

该代码的结果如下:

输入卷数:7

总计:2

总计:3#

总计:4

总计:5 ##

总计:6

总计:7 ###

总计:8

总计:9

总计:10#

总计:11

总计:12

答案 2 :(得分:0)

看起来你真的很亲密。您只需要为每个int变量打印#的数量。以下将对numTwos执行此操作:

         char[] chars = new char[numTwos];
         Arrays.fill(chars, '#');
         String result = new String(chars);
         System.out.println(result);

您可以将整个事物放在12的循环中,为所有这些打印。