• Post Reply Bookmark Topic Watch Topic
  • New Topic

Random Number with User Prompt  RSS feed

 
Diana Magers
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a good confident start on this but I think I am missing something here. I am wanting this program to prompt the user to enter a maximum value and a minimum value and the program should in theory generate a random number for the output. Here is my logic:



Then in my mail argument:


I am able to return both the user prompts and get the inputted answer to appear but I am not getting a response for the prompt "The random number is: ". It doesn't seem as though it would be this difficult. Sigh!

Can someone please give this program a glance and try to debug it. Thanks.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look carefully at lines 14 and 15. Just exactly how do you think that's going to output your result?
 
Diana Magers
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See now that is what I needed, someone to put me back on track. Thanks. I got it.

 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there are better ways to get “random” numbers in a certain range. The numbers from Random are really pseudo‑random. You will find a long discussion here, with more than one technique and with some pitfalls discussed. I haven't seen your technique before.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i would also suggest you separate out your code into methods. The way you get the input should be separate from how you generate your random number and that should be separate from printing it out.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I think there are better ways to get “random” numbers in a certain range...

@Diana: There are also better ways to get a "user based" random number, viz:or something like it. And then your main() code becomes simply:

UserRandom u = new UserRandom();
range = u.generate();


and you can also add whatever other restrictions you like.

Note that this is just one way of doing it; there are TONS of others - and you might not like mine - but Java is an Object-oriented language, so it's generally best to get objects (or classes) to do the job for you.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look very carefully at the documentation for that nextInt method, and the link I posted yesterday. Look very carefully and work out what the smallest and largest possible results from something like this:-Agree with Winston about that method; that is how I would do it.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!