• 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
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

How to add with "++" to a variable?  RSS feed

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I’m trying to write a program that “throws two dices” and sums both of them. The dices should be thrown 100 times, and every time the sum of the two should be calculated. Finally, I should print out how many times every sum occurred. For the sum 1, 2…12.

In my code I got a int array to store how many times each sum occurred. I just don’t know how to get it right. The line I’m referring to is commented in the code.  


 
author & internet detective
Posts: 39244
727
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel,s
The code you have commented out works. This simpler example prints [0, 2, 0]

 
Marshal
Posts: 64182
215
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Believe it or not, summa[a+b]++

You have a completely different error. The maximum score you can get on two dice is 12 but the maximum index of that array is 11. Try an array one larger.
Your indentation is worse than in your previous post.
 
Marshal
Posts: 6697
464
BSD Linux Mac OS X VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got some requirements, you got some code, but those don't match.
Java is an object oriented language, so really you supposed to aim to write this program in an object oriented way.

You have mentioned dice, so that's a hint what tha class may read as. Dice. Then you mentioned that such dice supposed to throw a number, well, maybe "roll" would be better.

So how about if you'd have such API?

Then in turn you could create more dices like that:

Then you easily could add dice.roll() part to a loop and roll as many times as you want, and in addition you could calculate sums that way.

Then you could create a mechanism to calculate how many times each of the sums appear. That way you'd have a bit more organized program instead of having everything in a Main class and main method.
 
Daniel Andersson
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:You got some requirements, you got some code, but those don't match.
Java is an object oriented language, so really you supposed to aim to write this program in an object oriented way.

You have mentioned dice, so that's a hint what tha class may read as. Dice. Then you mentioned that such dice supposed to throw a number, well, maybe "roll" would be better.


Thanks I will look at your code. I think it is ok not to write this assignment in an object oriented way since it belongs to the beginning of the course.
But you are right that would be better. I will see what the professor says tomorrow when its time for review.
Sorry I guess roll would have be better than throw, just my swenglish.
 
Liutauras Vilda
Marshal
Posts: 6697
464
BSD Linux Mac OS X VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Danielx Anderssonx wrote:I think it is ok not to write this assignment in an object oriented way since it belongs to the beginning of the course.


Perhaps, but don't spend too much time in such stage. Read about how to create objects, why they are for, so you earlier get introduced yourself to that.
 
Daniel Andersson
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Believe it or not, summa[a+b]++

You have a completely different error. The maximum score you can get on two dice is 12 but the maximum index of that array is 11. Try an array one larger.
Your indentation is worse than in your previous post.


Ok i will try that thanks
Sorry this is my first day of writing Java. I'm just struggling to make the program do what its asked to do.
What do you specially mean with  indentation? Is there a  rule of thumb I could follow to be better?
 
Liutauras Vilda
Marshal
Posts: 6697
464
BSD Linux Mac OS X VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Danielx Anderssonx wrote:What do you specially mean with  indentation? Is there a  rule of thumb I could follow to be better?


Yes. Document from April 20, 1999, quite old, but practices still widely appreciated among Java developers. Document is here.
Staff note (Liutauras Vilda):

Thank you for the pie slice!

 
Marshal
Posts: 13447
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was reading this thread and thought it might be cool to try to play around with some functional-style programming in Java.  

Look over this code and see if you can get any ideas: https://repl.it/@jlacar/FunctionalDice
 
Campbell Ritchie
Marshal
Posts: 64182
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have our own suggestions about formatting: there are two major differences from the link Liutauras showed you:-
  • 1: One document says to put { at the end of the line and the other on a line by itself. It is all right to do either, as long as you are consistent.
  • 2: One document says you are allowed tab characters and the other says not to use tabs. The one saying don't use tabs is correct.
  •  
    I am a man of mystery. Mostly because of this tiny ad:
    Create Edit Print & Convert PDF Using Free API with Java
    https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!