r/learnjava • • Jan 18 '23

Learning java at 44

I've decided to learn java so I'm doing a short free course on udacity. I'm quite enjoying but I got stuck on a very basic bit of code and need some help. I'm generating random numbers for dice but now need to change the number of sides from 6 to 8 using parameters. I gone over my notes but still not quite sure where I've gone wrong. This course doesn't give me the answer anywhere so I can't see where I've gone wrong. I know (int sides) are the parameters I have to use but not sure how. Some guidance please!!

Public int roll Dice (int sides){ randomNumber=Math.random(); randomNumber=randomNumber*6; randomNumber=randomNumber+1; int randomInt=(int)randomNumber;

return randomInt; }

43 Upvotes

18 comments sorted by

View all comments

16

u/wynand1004 Jan 18 '23

If you look at your code you have the number 6 in it. But now you want that to change based on the parameter sides. So, change the 6 to sides.

Good luck with your studies.

9

u/Primary-Reputation-2 Jan 18 '23

Ah of course. Thanks very much for your guidance

4

u/idhanjal Jan 18 '23

One thing to remember. Avoid using hard coded (like the 6 here in your code) anywhere. They always lead to unexplained behavior.