samedi 9 avril 2016

User Input Sided Dice Value Limiting to Only 6 sides

I am very new to java but I have, what I think is, a simple question. I already created a random generator for a 6 sided dice. This week I need to change that so the user can input how many sides the dice has. I have been able to get the input all set up but the system is stuck providing the results for 6 sides, even after I input 12 sides. It seems that I am missing a connection somewhere but can't find it. I do need to default the sides to 6 which I believe is what is causing the issue. Below is the code I have right now.

import java.util.Random;

public class Dice {
        private int numberShowing;
        private int numberOfSides;
        private static Random randomNumber = new Random();
 
        public Dice(){
                this(6);
                roll();
        }       
        
        public Dice(int sides){
                this.numberOfSides = sides;
        }
        
        public int getNumberOfSides(){
                return numberOfSides;
        }

        // Requested member function
        public int roll() {
                numberShowing = randomDiceRoll(1,numberOfSides);
                return numberShowing;
        }
        
        private int randomDiceRoll(int low, int high) {
                return randomNumber.nextInt(high - low + 1) + low;
        }
 
        public int getNumberShowing() {
                return numberShowing;
        }

        public String toString() {
                return "Dice is showing value "+numberShowing;
        }   
        
};
import java.util.Scanner;

public class DiceClient {

        static Scanner user_input = new Scanner(System.in);

        protected static int sides;
        
        public static final int ROLLS=6_000_000;
        
                public static void main(String[] args) {
                        Dice dice = new Dice();
                        
                System.out.println("How many sides will the die have?");
                Scanner keyboard = new Scanner(System.in);
                sides = keyboard.nextInt();
                        
                        
                int[] counts = new int[sides +1];       
                
        for (int i=0; i<ROLLS; i++) {
                        int showing = dice.roll();
                        counts[showing] ++; // The "0" index is not used.
        }
        printResults(counts);
}

                public static void printResults(int[] resultArray) {
                        for (int i=1; i<resultArray.length; i++) {
                                // System.out.println("Value: "+i+": "+resultArray[i]+" times");
        int numberOfTimes = resultArray[i];
        double percentage = numberOfTimes*100.0 / ROLLS;
        // Let's show off "printf"
                        System.out.printf("Value %2d: %10d times (%4.2f%%)\n", i, numberOfTimes, percentage);
                }
        }
}



Aucun commentaire:

Enregistrer un commentaire