Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Newbie  RSS feed

 
Brittney Wilcutt
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
New to Java and it will soon become obvious

Here is my assignment for my java class:

"Write a program that simulates rolling a single six-sided die. The user should be able to enter the number of rolls. After rolling the entered number of time,s the output will display how many times a one occurred, two occurred, up to how many times a six occurred."

We're working with loops. So far, this is what I have:




Any help is appreciated. Thanks
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You ask the user how many rolls but don't take any input. Suggest you look at Scanner class, specifically how to get user input from the keyboard.

Suggest you review the Random() constructors and the use of a Class' methods. (referring to your generatornextInt() statement ).

count is going to be the number of rolls desired by the user?
num should be the result of your random number generator.

The 'meat' of your while statement should be in curly brackets. As it is, with a semi-colon at the end, your while statement does nothing.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
without even looking at your code, I would make the following suggestions:

1) Write as little code as possible each time before you compile/fix/debug. Some developers hate writing more than ONE before going through this cycle.

2) Try and modularize your code. For example, rolling of a die is independent of getting user input. I would write a method that does nothing more than rolls a 6-sided die, and that returns the value of the roll. Test the heck out of it. when you are satisfied it works (and to test it you may have to write a bunch of code you eventually throw away), you know you can now use it elsewhere - for example, in a loop so it gets called more than once.

3) TEST TEST TEST. When you are done testing, test some more. Get a friend to test your code, and challenge them to break it. I have had code that I was SURE was 100% rock solid. I had someone test it, and they broke it on their first go, without even trying to do so.

4) Only try and do a piece at a time. Again, for example, don't worry about keeping track of the number of times a '1' comes up until your code rolls the die the correct number of times. Personally, I'd approach it like this:

version 0.1 get it to roll a die and print the result (again, with the die roll in an independent method from main()).
version 0.2 Get the user input and print it out (but don't do anything with it)
version 0.3 get it to get user input, roll the die that many times, and print out every roll.
version 0.4 get it to tally the number of times each roll occurs.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Let's have a look at the assignment.
Brittney Wilcutt wrote:
"Write a program that simulates rolling a single six-sided die. The user should be able to enter the number of rolls. After rolling the entered number of time,s the output will display how many times a one occurred, two occurred, up to how many times a six occurred."

Before starting to write any code, you must understand exactly what's required and you must have an idea of how to solve the problem with a computer program. Don't start writing code statements without understanding how you're going to solve the problem; that would be like writing a book without knowing how the story goes, just by writing some sentences.

Think about this first: How are you going to keep track of the number of times a 1, 2, 3, ..., 6 has occurred? You have to store the number of times each outcome has happened somewhere. What would be a good way to do that?

The main structure should be obvious: first you ask the user how many times to roll (print a message with the question and read the input from the keyboard), then there would be a loop for that number of times. Inside the loop you roll the die, check what the outcome is, and keep track of how many times that outcome has happened. Then, after the main loop, you print the results for each of the six possible outcomes.

Your code as it is now will go into an infinite loop because of line 18:

This line means: "as long as count is less than or equal to roll, do nothing" (the ; is an empty "do nothing" statement). You should put whatever you want to happen in the loop in a block delimited by curly braces, like this:

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!