Results 1 to 8 of 8
Thread: Please Help
- 02-03-2011, 09:39 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
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;
}
}
- 02-03-2011, 09:56 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
For starters read the API documentation for the Random class. Play special attention to the nextInt( ... ) method.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 02-03-2011, 10:12 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
I have read the random class but it's still not make sense to me !!
- 02-03-2011, 10:17 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 17
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.
- 02-03-2011, 10:39 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
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
- 02-03-2011, 10:45 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 17
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.
- 02-03-2011, 10:46 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
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 ...Build a wall around Donald Trump; I'll pay for it.
- 02-03-2011, 04:49 PM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 13
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();
Java Code:Random rand = new Random();
Bookmarks