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

Help with Dice generator  RSS feed

 
chris olejniczak
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


 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • 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!
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • 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
Java Python Ruby
  • 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: 55715
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I see what you mean
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!