ダブルサイコロの圧延プログラムのヒストグラムを印刷する必要性のヘルプ

Squanching:

私は、以前の質問に目を通していると、本当にこのアウトを把握することを試みました。私ははい、これは宿題ですので汚れがここに来て感じるが、私はまた私の教授に連絡しようとしてきました。応答なしとの最後の2週間。私はそれが簡単に修正だと確信していますし、私はそれを得ていないんだけど、私はしばらくの間、それをされていると立ち往生していましたいまいましいです。高度にありがとうございます。

私は、アスタリスクは、ここで全体の右側に印刷するように見えることはできません。それぞれの「*」は、全ロールの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);

しかし、* 'sが右の割合と一致し、アスタリスクのランダムな量を印刷するまで終わりません。

codeLover:

このようにしてみてください。

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

あなた* Sを印刷し終えた後、次の行を印刷

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=225354&siteId=1