Results 1 to 8 of 8

Thread: Please Help

  1. #1
    the-mask is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default Please Help

    Hi all ,

    I am having some difficulties to run the below program and willing for some help..

    -Create a method called countSixes in which you roll a die 100 times and count the number of sixes that result. The method should return (not output) the result. So if you invoke countSixes() and out of the 100 times, there were 23 sixes, your method should return 23.

    here's what i did so far !!



    public class Titel1

    {
    public static void main(String[] args)
    {

    // Create one dice, each with 6 sides



    public static int countSixes()
    {
    int die1 = new Die(6);

    countSixes = 0;

    for ( int i = 0 ; 100>i ;i++)
    {
    die1.roll();

    if (die1.conutSixes()== 6)



    return countSixes;


    }

    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,734
    Blog Entries
    7
    Rep Power
    21

    Default

    For starters read the API documentation for the Random class. Play special attention to the nextInt( ... ) method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    the-mask is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    I have read the random class but it's still not make sense to me !!

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    I would also suggest you study some basic material on the structure of a valid Java program. At the moment you are attempting to declare the countSixes() method within the main() method which isn't valid. Also the number of braces don't add up: they should come in matching pairs of { and }.

    Model the general program structure on the simplest examples you will have read about. And compile often, fixing the errors that the compiler tells you about as they arise.

    If you can't understand the compiler messages then do post back with your code and the message. Or, later, if you have some specific question about the API documentation of a method, ask that. But, as I'm sure you realise, no-one is actually going to write the code for you. (Or at least I hope they won't as that would deprive you of all the fun!)
    Last edited by pbrockway2; 02-03-2011 at 10:20 AM.

  5. #5
    the-mask is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    I have reedited my codes and i just got an error message i didn't understand it ,, also I am just wondering if i am so far so good with this progress


    public class Titel1

    {
    public static void main(String[] args)
    {
    countSixes();

    }

    // Create one dice, each with 6 sides



    public static int countSixes()
    {
    die1 = new Die(6);

    countSixes = 0;

    for (int i=0;i<100;i++)
    {

    return countSixes;


    }

    }


    ------------------------ here's the error i didn't understand !!

    C:\Users\Alex\Desktop\Titel1.java:29: reached end of file while parsing
    }
    ^
    1 error

    Tool completed with exit code 1

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    When you post code the thing is to put [code] at the start and [/code] at the end. That way it appears like this:

    Java Code:
    public class Titel1
    
    {
    	public static void main(String[] args)
    	{
    		countSixes();
    
    	}
    
    	// Create one dice, each with 6 sides
    
    
    
           public static int countSixes()
           {
    		    die1 = new Die(6);
    
                countSixes = 0;
    
           for (int i=0;i<100;i++)
           {
    
    		return countSixes;
    
    
    }
    
    }

    The error means that the braces don't match up. The compiler reaches the end of the file while still looking for a }.

    Don't just add a } randomly. Think about which { goes with which } and have them line up in your source code.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,734
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by the-mask View Post
    ------------------------ here's the error i didn't understand !!

    C:\Users\Alex\Desktop\Titel1.java:29: reached end of file while parsing
    }
    ^
    1 error
    Did you read what the compiler had to say? It was happily parsing your source code while all of a sudden there was no more input while the compiler expected some more. Count your curly brackets and see for yourself.

    kind regards,

    Jos

    edit: Jeez, too slow again; I need more coffee, more coffee ...
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    How you create the die item is also incorrect. The correct way is to create a reference, then create the object, then link them with =
    Java Code:
    Object o = new Object();
    Whats your die class look like? Using a Random object would probably be much simpler
    Java Code:
    Random rand = new Random();
    is the way it should look to create a random object. I'll leave the rest of the stuff required for random up to you.

Posting Permissions

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