• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Write a method calles diceSum

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I am having a little trouble understanding why I am not getting this right, I followed all the steps in the book and it still came out wrong, help please!

Write a method called diceSum that accepts a Scanner for the console as a parameter and prompts for a desired sum, then repeatedly simulates the rolling of 2 six-sided dice until their sum is the desired sum. Here is a sample dialogue with the user:

Desired dice sum: 9

4 and 3 = 7

3 and 5 = 8

5 and 6 = 11

5 and 6 = 11

1 and 5 = 6

6 and 3 = 9



So what am i not understanding?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, and welcome to the Ranch!

Folks here will be better able to help you if you TellTheDetails(←click) about exactly what's not working for you or what you're not understanding.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Off the bat, though, I will ask you to explain exactly why you put this line in and what you think it will do:

 
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You never read the desired result (i.e: 9 from the scanner).

You have hardcoded the "desired result" of 7 at line 11.

WP
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Here is a sample dialogue with the user:
...


It's certainly not from the code you posted, since that one can't even be compiled.
 
Sora McKain
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:Off the bat, though, I will ask you to explain exactly why you put this line in and what you think it will do:



Well I get an error that says

Dice.java:6: illegal start of expression
r.nextInt=();
^
1 error

so thought I might need that line but it obviously does no good.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That sounds like Programming By Accident. Not a particularly effective approach.

It's okay to guess sometimes, but if you do, make sure you at least have some expectation of what each piece means and why it might work.

So, for instance, when you write


don't just do it because you're sticking arbitrary symbols together. If you have a theory as to what each piece of that line might mean, and why you might need it, then sure, try it out, see if your theory holds up. But just reaching into the blind grab bag and throwing together whatever happens to come out--an - sign here, some parentheses there--is something to be avoided always.
 
Sora McKain
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all for your help, I have figured out my problem (I also figured out that this is not something I should be doing on little to no sleep lol).

Once again thank you. If I have problems in the future I will be sure to ask.
 
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic