2017-03-10 94 views
0
/** 
    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線所有骰子的輥。

我怎樣才能讓它只輸出每種類型的卷的總數是多少?

+0

你是否試圖輸出雙打或兩個骰子的所有組合的卷數? –

+0

只是雙打 – MrBlargg

回答

0

通過向文件中寫入不同的信息?

誰說,你必須打印到循環中的輸出文件?

你已經收集在該循環的計數器的統計數據(以打印他們在循環後)。就輸出文件做同樣的事情。

除此之外:瞭解陣列。當你開始使用諸如a1,a2等變量名時......通過使用數組,這樣的代碼總是變得更簡單。

0

如果我理解正確的話,你會想只是outputFile.close();

//add code that closes the output file for TASK #3 here///////////////// 
outputFile.println ("Snake eyes occurred a total of " + snakeEyes + " times"); 
outputFile.println ("Twos occurred a total of " + twos + " times"); 
outputFile.println ("Threes occurred a total of " + threes + " times"); 
outputFile.println ("Fours occurred a total of " + fours + " times"); 
outputFile.println ("Fives occurred a total of " + fives + " times"); 
outputFile.println ("Sixes occurred a total of " + sixes + " times"); 
outputFile.close(); 

我也建議去在你的代碼,並通過使用方法減少重複的代碼,並要求他們在必要時之前包括該代碼。

+0

謝謝。 Id早些時候嘗試過,但沒有與其他產出密切相關。 – MrBlargg

0

假設你正試圖輸出爲每雙輥,只需將打印到文件了while循環的代碼,使其打印你的蛇的眼睛,三三兩兩,三分球的最終結果,等