Results 1 to 3 of 3
  1. #1
    bmxriderss is offline Member
    Join Date
    Jan 2009
    Posts
    1
    Rep Power
    0

    Default help with loops with source code

    hi, i'm having trouble with this assignment.

    3. Determine how many times a die must be rolled in order to
    win a prize. (This represents one trial.) Print this value to a
    text file.
    4. Conduct at least 1,000 trials.
    5. Read the data back in from all of the trials.
    6. Calculate the average number of times a die must be rolled in order to win a prize.
    7. Print the result to the screen

    This is what I have so far.
    But there is something wrong with the loop.
    thanks

    Java Code:
    /**
     * Write a description of class BottleCapPrize here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Scanner;
    import java.util.Random;
    import java.io.File;
    public class BottleCapPrize
    {
        public static void main (String [ ] args) throws IOException
         {
            PrintWriter outFile = new PrintWriter(new File("bah.txt"));
            Random randomGenerator = new Random();
            int random = randomGenerator.nextInt(5);
            int count = 0;
            
        while(random != 3)
         {    
           for (int loop = 1; loop <= 1000; loop++)
            {
               System.out.println("About to check with random=" + random);
              if(random == 3) // the altered code
              {
                outFile.println("Congratulations, both pairs matched.");
                count++; 
               }
              outFile.close ( );
            
             }
          }
       } 
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Are you sure that you want to close your file from within the for loop?

  3. #3
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    Java Code:
    random = randomGenerator.nextInt(5);
    above code place outside the for-loop
    once random not equal to 3,
    you loop 1000 times, and tell you user, "sorry, you lost"?

Similar Threads

  1. Replies: 1
    Last Post: 11-28-2008, 06:27 PM
  2. Help me out in compiling the source code
    By aks.nitw in forum Advanced Java
    Replies: 3
    Last Post: 10-17-2008, 08:33 AM
  3. source code of 'javac'?
    By Pooja Deshpande in forum Advanced Java
    Replies: 2
    Last Post: 06-04-2008, 11:24 AM
  4. how to convert source code to xml
    By valery in forum XML
    Replies: 2
    Last Post: 08-06-2007, 08:29 PM
  5. Need a source code
    By vissu007 in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 07:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •