Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-12-2009, 01:48 AM
Member
 
Join Date: Jan 2009
Posts: 1
Rep Power: 0
bmxriderss is on a distinguished road
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

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 ( );
        
         }
      }
   } 
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 01-12-2009, 04:25 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 3,195
Rep Power: 5
Fubarable is on a distinguished road
Default
Are you sure that you want to close your file from within the for loop?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-12-2009, 09:56 AM
Senior Member
 
Join Date: Dec 2008
Location: Hong Kong
Posts: 393
Rep Power: 1
mtyoung is on a distinguished road
Default
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"?
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
MavenJava - browse source code of all open source projects online jirkacelak Java Announcements 1 11-28-2008 06:27 PM
Help me out in compiling the source code aks.nitw Advanced Java 3 10-17-2008 08:33 AM
source code of 'javac'? Pooja Deshpande Advanced Java 2 06-04-2008 11:24 AM
how to convert source code to xml valery XML 2 08-06-2007 08:29 PM
Need a source code vissu007 New To Java 1 07-05-2007 07:08 PM


All times are GMT +2. The time now is 02:39 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org