2016-11-30 48 views
0

我的班級有一個問題,我無法弄清楚。帶循環的骰子滾動直方圖

這是一個問題:

本測驗的目的是加強使用循環計數,以及檢討使用隨機數的理解。

修改下面的程序打印一個直方圖,其中骰子滾動的總次數等於每個可能值的總次數通過打印像#這樣的次數的字符來顯示。兩卷骰子將用於每卷。

示例:

直方圖顯示每個可能值的骰子總數。

骰子輥統計(結果而變化):

2S:######

3S:####

4S:###

5S:# #######

6S:###################

7S:############ #

787-8:#############

787-9:##############

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; 
     } 
    } 

任何幫助將是非常讚賞。

+0

乍一看:用數組替換numOnes,numTwos等是一件好事。另外:你能更清楚地知道你有什麼問題嗎?我猜在輸出中打印了正確數量的「#」。 – markspace

+1

如果你的問題是生成#的重複字符串,那麼這是一個http://stackoverflow.com/questions/7107297/what-is-the-easiest-way-to-generate-a-string- n重複字符 – sprinter

回答

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; 
    } 
} 

讓我知道你是否需要澄清問題。

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

Tota l:12

0

看起來你真的很接近。你只需要打印每個int變量的#號。以下將爲numTwos做到這一點:

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

你可以把整個東西放在一個12的循環中打印它們的全部。

+0

我該把這個放在哪裏? –

+0

代替該線的: – Abdulgood89

+0

代替該線的: – Abdulgood89