骰子滚动直方图

时间:2015-02-28 03:56:32

标签: java histogram dice

我有一个类问题,我似乎无法理解其逻辑。

修改下面的程序以打印直方图,其中骰子滚动的总次数等于每个可能的值,通过打印像*那样的字符来显示,如下所示。

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

骰子滚动直方图:

2:  ******
3:  ****
4:  ***
5:  ********
6:  *******************
7:  *************
8:  *************
9:  **************
10: ***********
11: *****
12: ****

我不明白这个直方图显示的是什么。它让我疯了,如果我不明白我在做什么,我无法继续我的逻辑。我知道之前已经问过这个问题,但看起来他们都有特定数量的骰子角色。我没有得到一个数字,这就是让我失望的原因。我知道这真是一个愚蠢的问题。但任何人都可以向我解释他们的意思"直方图显示每个可能值的骰子总数"什么定义了这个可能的价值?我完全失去了...任何帮助表示赞赏。这是我到目前为止编写的代码。

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 i = 0;            // Loop counter iterates numRolls times

      int numRolls = 0;     // User defined number of rolls

      int numOnes = 0;      // Tracks number of 1's found
      int numTwos = 0;      // Tracks number of 2's found
      int numThrees = 0;    // Tracks number of 3's found
      int numFours = 0;     // Tracks number of 4's found
      int numFives = 0;     // Tracks number of 5's found
      int numSixes = 0;     // Tracks number of 6's found
      int numSevens = 0;    // Tracks number of 7's found
      int numEights = 0;    // Tracks number of 8's found
      int numNines = 0;     // Tracks number of 9's found
      int numTens = 0;      // Tracks number of 10's found
      int numElevens = 0;   // Tracks number of 11's found
      int numTwelves = 0;   // Tracks number of 12's found

      int die1 = 0;         // Dice values
      int die2 = 0;         // Dice 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;

            // Count number of sixes and sevens
            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("Roll " + (i+1) + " is " + rollTotal + " (" + die1 + 
                  "+" + die2 + ")");

         }

         // Prints a histogram of the number of dice rolls
         System.out.println("\nDice roll histogram:");
         System.out.println("1's: " + numOnes);
         System.out.println("2's: " + numTwos);
         System.out.println("3's: " + numThrees);
         System.out.println("4's: " + numFours);
         System.out.println("5's: " + numFives);
         System.out.println("6's: " + numSixes);
         System.out.println("7's: " + numSevens);
         System.out.println("8's: " + numEights);
         System.out.println("9's: " + numNines);
         System.out.println("10's: " + numTens);
         System.out.println("11's: " + numElevens);
         System.out.println("12's: " + numTwelves);
      }
      else {
         System.out.println("Invalid rolls. Try again.");
      }

     return;
   }
}

您可以看到我输入直方图条目的位置。我基本上认为我需要编辑从整数中给出的值,例如&#34; numOnes&#34;到星号,但现在我不确定......任何帮助都表示赞赏!

编辑代码 -

System.out.println("\nDice roll histogram:");


     System.out.print("2's: ");
     for(i = 0; i < numTwos; i++){
         System.out.print("*");
     }
     System.out.println("");

     System.out.print("3's: ");
     for (i = 0; i < numThrees; i++);{
         System.out.print("*");
     }
     System.out.println("");

     System.out.print("4's: " );
     for (i = 0; i < numFours; i++);{
         System.out.print("*");
     }
     System.out.println("");

     System.out.print("5's: ");
     for (i = 0; i < numFives; i++);{
         System.out.print("*");
     }
     System.out.println("");

     System.out.print("6's: ");
     for (i = 0; i < numSixes; i++);{
         System.out.print("*");
     }
     System.out.println("");

     System.out.print("7's: ");
     for (i = 0; i < numSevens; i++);{
         System.out.print("*");
     }
     System.out.println("");

     System.out.print("8's: ");
     for (i = 0; i < numEights; i++);{
         System.out.print("*");
     }
     System.out.println("");

     System.out.print("9's: ");
     for (i = 0; i < numNines; i++);{
         System.out.print("*");
     }
     System.out.println("");

     System.out.print("10's: ");
     for (i = 0; i < numTens; i++);{
         System.out.print("*");
     }
     System.out.println("");

     System.out.print("11's: ");
     for (i = 0; i < numElevens; i++);{
         System.out.print("*");
     }
     System.out.println("");

     System.out.print("12's: ");
     for (i = 0; i < numTwelves; i++);{
         System.out.print("*");
     }
     System.out.println("");

  }

输出 -

Enter number of rolls: 
5
Roll 1 is 8 (2+6)
Roll 2 is 9 (6+3)
Roll 3 is 9 (5+4)
Roll 4 is 6 (4+2)
Roll 5 is 9 (6+3)

Dice roll histogram:
2's: 
3's: *
4's: *
5's: *
6's: *
7's: *
8's: *
9's: *
10's: *
11's: *
12's: *

我可以输入更多的卷,我会得到一个星号,它会像我需要的那样增加,但它不会增加其余的数字而且它们都只能得到一个星号。是什么让我的代码不能正确增加*的数量?我已经打了好几个小时了:/

2 个答案:

答案 0 :(得分:5)

  

&#34;我不明白这个直方图显示的是什么。&#34;

好吧,正如你所说:

  

&#34;直方图显示每个可能值的骰子总数&#34;

直方图:

2:  ****** 
3:  **** 
4:  *** 
5:  ******** 
6:  ******************* 
7:  ************* 
8:  ************* 
9:  ************** 
10: *********** 
11: ***** 
12: ****

是一对六面骰子的卷的记录。总共一对骰子的可能值范围从2到12.如果你将它们滚动100次,它们可能会给出这些结果。

此直方图显示值6最常滚动(19次),4次最不频繁滚动(3次)。如果你计算每一个*,你就知道掷骰子的次数。

您可能认为直方图必须如下所示:

enter image description here

但转向侧面:

enter image description here

并想象蓝条是*100 - 150标签是2150 - 200标签是{{1}然后它开始看起来像你的直方图。

任何一种形式都是直方图。它们可以让您比较不同类别(种类)事物的数量(多少)。

图形图很难从基于文本的程序中输出,因此它要求您显示基本的ascii艺术:

enter image description here

如果这是在文本文件中,而不是程序输出,这也可以被认为是一种形式。您可以在收到新数据时轻松添加新的*。它与此相同:

enter image description here

希望更有意义。

SPOILER ALERT:如果这有帮助,并且您想修改程序,那么请不要再进一步阅读。否则...

3

和这个

     System.out.println("\nDice roll histogram:");
     System.out.println(" 1's: " + nManyStars(numOnes));
     System.out.println(" 2's: " + nManyStars(numTwos));
     System.out.println(" 3's: " + nManyStars(numThrees));
     System.out.println(" 4's: " + nManyStars(numFours));
     System.out.println(" 5's: " + nManyStars(numFives));
     System.out.println(" 6's: " + nManyStars(numSixes));
     System.out.println(" 7's: " + nManyStars(numSevens));
     System.out.println(" 8's: " + nManyStars(numEights));
     System.out.println(" 9's: " + nManyStars(numNines));
     System.out.println("10's: " + nManyStars(numTens));
     System.out.println("11's: " + nManyStars(numElevens));
     System.out.println("12's: " + nManyStars(numTwelves));

如果放在正确的位置,应该让你的程序停止打印它现在打印的内容,这是:

   static String nManyStarsOld(int n) {
       String result = "";

       for (int i = 0; i < n; i++) {
           result += "*";
       }

       return result;
   } 

相反,让它打印出来:

Enter number of rolls: 
20
Roll 1 is 5 (2+3)
Roll 2 is 10 (4+6)
Roll 3 is 7 (5+2)
Roll 4 is 7 (5+2)
Roll 5 is 5 (1+4)
Roll 6 is 11 (6+5)
Roll 7 is 8 (3+5)
Roll 8 is 8 (2+6)
Roll 9 is 7 (1+6)
Roll 10 is 8 (3+5)
Roll 11 is 12 (6+6)
Roll 12 is 6 (2+4)
Roll 13 is 8 (6+2)
Roll 14 is 8 (4+4)
Roll 15 is 7 (1+6)
Roll 16 is 4 (2+2)
Roll 17 is 6 (5+1)
Roll 18 is 7 (6+1)
Roll 19 is 6 (1+5)
Roll 20 is 5 (1+4)

Dice roll histogram:
1's: 0
2's: 0
3's: 0
4's: 1
5's: 3
6's: 3
7's: 5
8's: 5
9's: 0
10's: 1
11's: 1
12's: 1

下次发布问题时,请包含当前输出以及预期输出。

如果你觉得自己是一个聪明的裤子,实际上有一个版本的nManyStars():

Dice roll histogram:
 1's: 
 2's: 
 3's: 
 4's: *
 5's: ***
 6's: ***
 7's: *****
 8's: *****
 9's: 
10's: *
11's: *
12's: *

灵感来自:https://stackoverflow.com/a/4903603/1493294

此外,如果您至少拥有Java 11,则可以 static String nManyStars(int n) { return new String(new char[n]).replace("\0", "*"); } 换取nManyStars(n)

希望有所帮助

答案 1 :(得分:0)

我已经完成了所有工作。感谢@CandiedOrange和@MadProgrammer的帮助和指导!发现我的直方图代码搞砸了我的for语句有一个;在一个不正确的位置导致我的for语句不应用*基于其运行for语句的过程,它只是打印*作为正常的打印功能。一切都很好。谢谢你的帮助!