Java 骰子滚动直方图

Java 骰子滚动直方图,java,histogram,dice,Java,Histogram,Dice,我有一个课堂问题,我似乎无法理解其中的逻辑 修改下面的程序,打印一个直方图,其中骰子滚动的总次数等于每个可能的值,通过打印一个类似*的字符来显示,如下图所示 显示每个可能值的骰子卷总数的直方图 骰子滚动直方图: 2: ****** 3: **** 4: *** 5: ******** 6: ******************* 7: ************* 8: ************* 9: ************** 10: *********** 11: ****

我有一个课堂问题,我似乎无法理解其中的逻辑

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

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

骰子滚动直方图:

2:  ******
3:  ****
4:  ***
5:  ********
6:  *******************
7:  *************
8:  *************
9:  **************
10: ***********
11: *****
12: ****
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;
   }
}
我可以输入更多的卷数,我会得到一个2的星号,它会像我需要的那样增加,但不会增加其余的数字,它们都只有一个星号。是什么阻止我的代码正确增加*?我已经为此奋斗了几个小时:/

“我不明白这个直方图到底显示了什么。”

正如你所说:

“显示每个可能值的骰子卷总数的直方图”

直方图:

2:  ******
3:  ****
4:  ***
5:  ********
6:  *******************
7:  *************
8:  *************
9:  **************
10: ***********
11: *****
12: ****
2:  ****** 
3:  **** 
4:  *** 
5:  ******** 
6:  ******************* 
7:  ************* 
8:  ************* 
9:  ************** 
10: *********** 
11: ***** 
12: ****
是一对六面骰子的掷骰记录。合计一对骰子的可能值范围为2到12。如果你将它们滚动100次,它们可能会给出这些结果

此直方图显示值6的滚动频率最高(19次),值4的滚动频率最低(3次)。如果你把每个*都数一数,你就会知道掷骰子的次数了

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

但要从侧面看:

想象一下,蓝色条是
*
100-150
标签是
2
,而
150-200
标签是
3
,那么它开始看起来像你的直方图

这两种形式都是直方图。它们可以让你比较属于不同类别(种类)的事物的数量(数量)

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

如果这是一个文本文件,而不是程序输出,这也可以被视为一种计数形式。您可以在收到新数据时轻松添加新*。这和下面一样:

希望这更有意义

剧透警告:如果这有帮助,你想尝试修改程序自己阅读没有进一步。否则

这个

还有这个

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

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

       return result;
   } 
相反,让它打印以下内容:

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: *
下次发布问题时,请包括当前输出和预期输出

如果你想成为一个聪明的人,实际上有一个单行版本的nManyStars():

受此启发:

另外,如果您至少有Java 11,您可以用
nManyStars(n)
换取
“*”。重复(n)

希望能有帮助

“我不明白这个直方图到底显示了什么。”

正如你所说:

“显示每个可能值的骰子卷总数的直方图”

直方图:

2:  ******
3:  ****
4:  ***
5:  ********
6:  *******************
7:  *************
8:  *************
9:  **************
10: ***********
11: *****
12: ****
2:  ****** 
3:  **** 
4:  *** 
5:  ******** 
6:  ******************* 
7:  ************* 
8:  ************* 
9:  ************** 
10: *********** 
11: ***** 
12: ****
是一对六面骰子的掷骰记录。合计一对骰子的可能值范围为2到12。如果你将它们滚动100次,它们可能会给出这些结果

此直方图显示值6的滚动频率最高(19次),值4的滚动频率最低(3次)。如果你把每个*都数一数,你就会知道掷骰子的次数了

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

但要从侧面看:

想象一下,蓝色条是
*
100-150
标签是
2
,而
150-200
标签是
3
,那么它开始看起来像你的直方图

这两种形式都是直方图。它们可以让你比较属于不同类别(种类)的事物的数量(数量)

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

如果这是一个文本文件,而不是程序输出,这也可以被视为一种计数形式。您可以在收到新数据时轻松添加新*。这和下面一样:

希望这更有意义

剧透警告:如果这有帮助,你想尝试修改程序自己阅读没有进一步。否则

这个

还有这个

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

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

       return result;
   } 
相反,让它打印以下内容:

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: *
下次发布问题时,请包括当前输出和预期输出

如果你想成为一个聪明的人,实际上有一个单行版本的nManyStars():

受此启发:

另外,如果您至少有Java 11,您可以用
nManyStars(n)
换取
“*”。重复(n)


希望它能帮上忙。我已经把它做好了。感谢@CandiedOrange和@MadProgrammer的帮助和指导!发现我的柱状图代码与我的for语句有误;在一个不正确的位置,导致我的for语句不应用*基于它运行for语句的过程,它只是将*作为一个正常的打印功能打印出来。一切都很好。谢谢你的帮助

我把一切都搞定了。感谢@CandiedOrange和@MadProgrammer的帮助和指导!发现我的直方图代码被弄乱了