lundi 9 février 2015

Calculate sum of java dice rolling program

I have a program that picks random numbers between one and six. It asks the user how many times it would like to roll the die and then outputs the results. It also lists how many times each number occurred. The final part of it is calculating the sum of all the random numbers. I can't seem to figure that out. If someone could help point me in the right direction I would be extremely thankful.



// Import statements
import java.util.Scanner;
import java.util.Random;

public class RandomDie {
public static void main(String[] args) {


Random x = new Random();

//Declare the variables that will be used
int roll = 0;
int total = 0;


//Creat array to store the random numbers
int randomArray[] = new int[1];
int countArray[] = new int[7];

//Introduce the program
System.out.println("Welcome to Random Die Rolls");

// Scanner class
Scanner scannerIn = new Scanner (System.in);

// Prompt how many times to roll the die
System.out.println("How many times would you like to roll the die?");
roll = scannerIn.nextInt();

// Roll the die however many times was indicated
// Display a thank you message for rolling the die and
// indicate how many times the die was rolled

System.out.println("Thank you! The die roll results for " + roll + " rolls were:");

//Start the counter at 0 and begin the for loop
int count=0;
for (count = 0;count<roll;count++) { //Begin for loop


// Generate some random numbers
// A random roll between 0 and 6
int randRoll = (int) (Math.random() * 6) +1;


//Display the results of the roll
System.out.println(+ randRoll);

//Store the numbers into the array
for (int r : randomArray)
countArray[randRoll]++;





} //end for loop

System.out.println("\nThe number of occurrences of each face were as follows:");

//begin counting number occurrences
for (int r = 1; r < countArray.length; r++) {
System.out.println("" + r + " occurs " + countArray[r] + " times");

} //end counting how many times a number occurs


//Calculate Sum
int[] values = new int[count];
for(int i = 0; i < count; i++) {
int randomInt = x.nextInt(count);
values[i] = randomInt;
}

int sum = 0;
for(int i : values) {
sum += i;
}
System.out.println(" Sum = " + sum);



} //end main
} //end class




Aucun commentaire:

Enregistrer un commentaire