I am new to java and have been asked to write a program that will generate random numbers between 10 and 20 and then stop when 18 is generated. Have been told to use a while statement and also to use Maths.Random and Maths.Round
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
I am new to java and have been asked to write a program that will generate random numbers between 10 and 20 and then stop when 18 is generated. Have been told to use a while statement and also to use Maths.Random and Maths.Round
Hello java3,
You must of learnt the basics by now? Please create a class and show us the code you have attempted.
Show us where you are stuck and we can then go from there.
Heres some help with the while loop: http://www.javaprogrammingforums.com...ava-loops.html
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
Ok this is what I've done. Like I said I am a complete novice to programming! When I run it it just keeps generating numbers non stop.
I was wanting the number generator to stop when an 8 was produced
Limit your base case to confirm your algorithm. Basically, your algorithm could theoretically generate numbers forever if it never returns an 8.
So, to test your algorithm, instead of a base case of nNum != 8, try something like nNum < 3. This way you have a much more narrow window for the loop continuing and if the loop breaks you can confirm that the algorithm you have for generating a random number (larger than 2) is correct. If that never finishes, then we most likely have an algorithm issue since a number being generated that is larger than 2 is much more likely than the number 8 being generated.
NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:
When asking for help, please follow these guidelines to receive better and more prompt help:
1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
2. Give full details of errors and provide us with as much information about the situation as possible.
3. Give us an example of what the output should look like when done correctly.
Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/