文件输出随机数

时间:2017-03-10 02:00:19

标签: java

    /**
    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中。

输出工作正常,但是当它输出时输出每个卷的实例,所以当输出所有实例时,所有掷骰子的卷都有10,000行。

如何让它仅输出每种卷筒的总数?

3 个答案:

答案 0 :(得分:0)

通过向该文件写入不同的信息?!

谁说您必须在该循环中打印到输出文件?

您已经在该循环中收集计数器统计信息(在循环之后打印它们)。就输出文件做同样的事情。

除此之外:阅读有关数组的内容。当你开始使用变量名,如a1,a2等......这样的代码总是通过使用数组变得更加简单。

答案 1 :(得分: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();

我还建议您使用方法检查代码并减少重复代码,并在必要时调用它们。

答案 2 :(得分:0)

假设您正在尝试为每个双卷输出,只需将打印的代码移出while循环,然后打印出蛇眼,二三,三等的最终结果。