/**
This class simulates rolling a pair of dice 10,000 times and
counts the number of times doubles of are rolled for each different
pair of doubles.
*/
import java.util.Random; //to use the random number generator
import java.util.Scanner;
import java.io.*;
import org.omg.PortableServer.ImplicitActivationPolicyOperations;
public class DiceSimulation
{
public static void main(String[] args) throws IOException
{
Scanner keyboard = new Scanner(System.in);
final int NUMBER = 10000; //the number of times to roll the dice
//a random number generator used in simulating rolling a dice
Random generator = new Random();
int die1Value; // number of spots on the first die
int die2Value; // number of spots on the second die
int count = 0; // number of times the dice were rolled
int snakeEyes = 0; // number of times snake eyes is rolled
int twos = 0; // number of times double two is rolled
int threes = 0; // number of times double three is rolled
int fours = 0; // number of times double four is rolled
int fives = 0; // number of times double five is rolled
int sixes = 0; // number of times double six is rolled
String filename; //name of file to write/read
int accumulator = 0;
double mean = 0;
int count2 = 0;
String userInput;
// add code for TASK #3
System.out.println("Please enter a specific path to a file");
filename = keyboard.nextLine();
PrintWriter outputFile = new PrintWriter(filename);
//loop while count is less than NUMBER of times
//adjust code in while loop as instructed in TASK #3
while(count < NUMBER)
{
//roll the dice
die1Value = generator.nextInt(6)+1;
die2Value = generator.nextInt(6)+1;
//Check to see if you have doubles
//Enter code for Task 1 if statements here
if (die1Value == die2Value);
if (die1Value == 1){
snakeEyes++;
outputFile.println ("Snake eyes occurred a total of " + snakeEyes + " times");}
else if(die1Value == 2){
twos++;}
else if(die1Value == 3){
threes++;}
else if(die1Value == 4){
fours++;}
else if(die1Value == 5){
fives++;}
else if(die1Value == 6){
sixes++;}
count++;
} //while(count < NUMBER) ending braces;
//add code that closes the output file for TASK #3 here/////////////////
outputFile.close();
System.out.println ("Output for while loop");
System.out.println ("You rolled snake eyes " + snakeEyes +
" out of " + count + " rolls.");
System.out.println ("You rolled double twos " + twos +
" out of " + count + " rolls.");
System.out.println ("You rolled double threes " + threes +
" out of " + count + " rolls.");
System.out.println ("You rolled double fours " + fours +
" out of " + count + " rolls.");
System.out.println ("You rolled double fives " + fives +
" out of " + count + " rolls.");
System.out.println ("You rolled double sixes " + sixes +
" out of " + count + " rolls.");
//Enter code for TASK #2 here for do while loop
do{
System.out.println("Count: " + count);
} while (count < NUMBER);
//roll the dice
die1Value = generator.nextInt(6)+1;
die2Value = generator.nextInt(6)+1;
//Check to see if you have doubles
//Enter code for Task 1 if statements here
System.out.println ("\nOutput for do while loop\n");
System.out.println ("You rolled snake eyes " + snakeEyes +
" out of " + count + " rolls.");
System.out.println ("You rolled double twos " + twos +
" out of " + count + " rolls.");
System.out.println ("You rolled double threes " + threes +
" out of " + count + " rolls.");
System.out.println ("You rolled double fours " + fours +
" out of " + count + " rolls.");
System.out.println ("You rolled double fives " + fives +
" out of " + count + " rolls.");
System.out.println ("You rolled double sixes " + sixes +
" out of " + count + " rolls.");
// Enter code for TASK #2 here for for loop
for(count = 1; count <= NUMBER; count++)
break;
System.out.println ("Output for, for loop\n");
System.out.println ("You rolled snake eyes " + snakeEyes +
" out of " + count + " rolls.");
System.out.println ("You rolled double twos " + twos +
" out of " + count + " rolls.");
System.out.println ("You rolled double threes " + threes +
" out of " + count + " rolls.");
System.out.println ("You rolled double fours " + fours +
" out of " + count + " rolls.");
System.out.println ("You rolled double fives " + fives +
" out of " + count + " rolls.");
System.out.println ("You rolled double sixes " + sixes +
" out of " + count + " rolls.");
// add code for TASK #4 here
}
}
中,我堅持在第一while
語句,其中我想輸出不同的輥的結果到一個單獨的.txt
點。文件輸出與隨機數
的輸出工作得很好,但是當它通過它輸出輥的每一個實例,因此,當所有實例都輸出有10000線所有骰子的輥。
我怎樣才能讓它只輸出每種類型的卷的總數是多少?
你是否試圖輸出雙打或兩個骰子的所有組合的卷數? –
只是雙打 – MrBlargg