Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Help with Dice generator

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I need to be able to input the number of rolls in JOptionPane that is my only problem


 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can check here how to get user input with JOptionPane.

But first I would suggest you reconsider the existing solution. First of all, it's never a good approach to mix GUI with console input/output, which is what you did. You could also check the design of your class, not everything should be in the main method. Declaring 12 variables of type int could probably be replaced with one int[] array of size 12. Math.random() returns double value (did you check the results you get?), so you should consider using Random class that contains method for generating int values that would be more appropriate for dices. That switch also doesn't make sense to me; first, you have no break statements, which means all of subsequent cases are always executed (maybe that was what you want, but it doesn't seem to me). And also, what's the point of putting case 1: when it's not possible to have the sum of two dice result equal to 1 (the least is 2, I guess)?

Edit: And please put the code inside code tags, and remove those extra blank lines you have so it will be easier to read. Check UseCodeTags (<- click).

Welcome to the Ranch!
 
Marshal
Posts: 64653
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kemal Sokolovic wrote: . . . Edit: And please put the code inside code tags, and remove those extra blank lines you have so it will be easier to read. Check UseCodeTags . . .

He did use code tags, but not correctly. I have altered them and got rid of some of the blank lines, and we can see how much better it looks.

And welcome again
I do not like the declaration of all twelve ints in the same line. Nor do I like seeing static fields, least of all if you “had to make them static to keep the compiler quiet.”
 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

He did use code tags, but not correctly.


I know, but the code was outside code tags. That's why I wrote to "put the code inside".
 
Campbell Ritchie
Marshal
Posts: 64653
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I see what you mean
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!