需要帮助为双骰子滚动程序打印直方图

时间:2018-10-15 04:20:02

标签: java

我仔细阅读了之前的问题,并确实尝试解决了这个问题。我来这很脏,因为是的,这是家庭作业,但我也尝试与教授联系。在过去的两个星期中没有回复。我敢肯定这是一个简单的解决方法,但我没有得到它,但是该死的我已经呆了一段时间了,被卡住了。在此先感谢您。

我似乎无法在所有总计的右侧打印星号。每个“ *”代表总卷数的1%。我想我已经拥有了所有东西,但是我还无法弄清楚如何在右侧打印它们。

所以代替;

2:****
3:******
4:**

等等。

我明白了

****2:
**3:
*****4:

等等。

这是我的代码:

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

    public class DoubleDiceRoll
    {
    public static void main(String[] args)
    {
  Random r = new Random();
  Scanner in = new Scanner (System.in);


  int[] frequency = new int[13];//Recording the number of times a number was rolled
  int numRolls = 0;//How many rolls the user wants to simulate
  int numAsterisks = 0; 
  int dieOne = 0;//Roll of die one
  int dieTwo = 0;//Roll of die two
  int rollTotal = 0;//Sum of the rolls of die one and two
  String stars = ""; 

  //Welcom user to the program
  System.out.println("Welcome to the dice throwing simulator!");

  System.out.println(" ");

  System.out.println("How many dice rolls would you like to simulate?");
  numRolls = in.nextInt();

  //Simulate the number of rolls for die 1 and 2

  for(int i = 0; i < numRolls; i++)
  {

     //Roll dieOne
     dieOne = r.nextInt(6) +1;

     //Roll dieTwo
     dieTwo = r.nextInt(6) +1;

     rollTotal = dieOne + dieTwo; 

     frequency[rollTotal]++;

  }//end for

  //Print Results

  System.out.println("DICE ROLLING SIMULATION RESULTS" + "\n" + "Each \"*\" represents 1% of the total number of rolls." + "\n"
                          + "Total number of rolls: " + numRolls);

  System.out.println("");//Space between text and results                       

  //Create for loop for print statement
  for(int total = 2; total < frequency.length; total++)
  {  
     numAsterisks = 100 * frequency[total] / numRolls; 


     for(int j = 0; j < numAsterisks; j++)
     {
         System.out.println("*");
     }
     System.out.println(total + ": ");
  }


   }


}

我知道我将*设置为在总数之前打印,但是我尝试打印的所有其他方式似乎会使它更加混乱。我将*设置为等于以下字符串:

for(int j = 0; j < numAsterisks; j++)
 {
   stars += "*";
 }
System.out.print (total + stars);

但是*与正确的百分比不匹配,最终会打印出随机数量的星号。

3 个答案:

答案 0 :(得分:2)

尝试这样:

Middleware

打印完* s后打印下一行

答案 1 :(得分:2)

您可能想先使用System.out.print()

含义:首先打印个总数,然后 println 个星号(一次拍摄)。

这将为您提供正确的顺序,并在您需要的位置添加换行符!

仅供参考:Java 11提供了String.repeat(),使您可以执行类似"*".repeat(numAsterisks);“的操作来生成每行所需数量的星号字符。

答案 2 :(得分:2)

简单的解决方案是使用System.out.print(而不是println),它将在当前行上打印输出,例如...

System.out.print(total + ": ");
for (int j = 0; j < numAsterisks; j++) {
    System.out.print("*");
}
System.out.println("");

稍微高级一点的解决方案是利用Java的String格式设置功能

System.out.print(String.format("%2d: ", total));
for (int j = 0; j < numAsterisks; j++) {
    System.out.print("*");
}
System.out.println("");

将列对齐...

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

更高级的解决方案可能使用StringBuilder

StringBuilder sb = new StringBuilder(128);
sb.append(String.format("%2d: ", total));
for (int j = 0; j < numAsterisks; j++) {
    sb.append("*");
}
System.out.println(sb.toString());