2016-11-30 9 views
0

Ich habe ein Problem in meiner Klasse, die ich einfach nicht herausfinden kann.Würfel Rollen Histogramm mit Loops

Das ist die Frage:

Der Zweck dieses Quiz ist es, das Verständnis für die Verwendung von Schleifen und Zählen sowie der Überprüfung der Verwendung von Zufallszahlen zu verstärken.

Ändern Sie das unten stehende Programm, um ein Histogramm zu drucken, in dem die Gesamtzahl der Würfeln aller möglichen Werte angezeigt wird, indem ein Zeichen wie # so oft gedruckt wird. Zwei Würfel werden in jeder Rolle verwendet.

Beispiel:

Histogram Gesamtzahl der Würfelrollen für jeden möglichen Wert zeigt.

würfeln Statistik (Ergebnis variiert):

2s: ######

3s: ####

4s: ###

5s: # #######

6s: ###################

7s: ############ #

8s: #############

9s: ##############

10s: ##### ######

11s: #####

12s: ####

~~~~~~~~~~~~~~~~~~~ ~~

Ich war nicht in der Lage, das Programm zu bekommen, das Histogramm im Beispiel zu drucken le oben.

Und das ist, was ich habe, so weit:

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

Jede Hilfe sehr geschätzt werden würde.

+0

Erster Blick: Es wäre gut, 'numOnes',' numTwos' usw. durch ein Array zu ersetzen. Außerdem: Könnten Sie genauer erklären, welches Problem Sie haben? Ich schätze, die richtige Anzahl von "#" in der Ausgabe zu drucken. – markspace

+1

Wenn Ihr Problem eine sich wiederholende Zeichenfolge von # erzeugt, dann ist dies ein Duplikat von http://stackoverflow.com/questions/7107297/what-is-the-easiest-way-to-generate-a-string- von-n-wiederholten Zeichen – sprinter

Antwort

0

Haben Sie eine Schleife wie diese, wo Sie Ihre Druckanweisungen haben.

Ändern Sie Ihren Code so, dass Sie nicht jedes Mal neue Variablen in ein Array aufnehmen müssen, damit Sie sie durchlaufen können.

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

Lassen Sie mich wissen, wenn Sie eine Klärung des Problems benötigen.

0

Sie können etwas tun:

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

Dieser Code wird wie unten ein Ergebnis haben:

Geben Sie die Anzahl der Rollen: 7

Gesamt: 2

Total: 3 #

Gesamt: 4

Total: 5 ##

Insgesamt wurden 6

Insgesamt wurden 7 ###

Insgesamt wurden 8

Insgesamt wurden 9

Total: 10 #

Gesamt: 11

Tota l: 12

0

Sieht aus wie du bist wirklich nah dran. Sie müssen nur die Anzahl von # für jede int-Variable, die Sie haben, drucken. Im Folgenden wird das tun, für die numTwos:

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

Sie können in einer Schleife von 12 die ganze Sache steckte es für sie alle zu drucken.

+0

Wo würde ich das hinstellen? –

+0

Anstelle dieser Linie: – Abdulgood89

+0

Statt dieser Zeile: – Abdulgood89