Bookmark Topic Watch Topic
  • New Topic

A while statement isn't executing something properly in my code.  RSS feed

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hello!

I've made a simple code which "throws" dice and the goal is to reach a certain sum with X dice. The code maybe is a little too long, but the important part lies at line 70:
It is still work in progress, but I want to get the main part of the program finished, but the problem is, even if total = sum1, the program keeps running, which it shouldn't, what's the issue here?





Thanks for you time and help!
 
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Report post to moderator
First, stop coding. You are heading in the wrong direction. This is not Object Oriented code, it's procedural. There is not a single object in there. Your main method should have no more than 3 or 4 lines. you should have a Die or Dice object somewhere. You need to step back from this and search these forums for Dice programs. It's a very common exercise and you'll find lots of good advice on how to approach it, but right now you need to heed the words of a wise man who said "When you find yourself in a hole, the first thing to do is stop digging!".
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Report post to moderator
Also, I cannot find a while loop. I did find a do loop, but that behaves differently from a while loop. It's all in the Java Tutorials..
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Report post to moderator
Campbell Ritchie wrote:Also, I cannot find a while loop. I did find a do loop, but that behaves differently from a while loop. It's all in the Java Tutorials..

There is a while statement on line 25, but it's not a loop because it doesn't do anything due to a syntax error. There are a lot of issues with this code. When/if the OP comes back we'll help him through 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
  • Report post to moderator
I had an urgent project on my desk this morning, but it's in production now so I have a bit more time. I did some searching and I think you might find these threads helpful.

dice tracking

rolling dice

dice part 2

You might also want to read this -> StopCoding
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Report post to moderator
J. Kevin Robbins wrote: . . . There is a while statement on line 25, . . . .
[pedantic mode]That isn't a while statement, but the end of a do loop. Well, officiallly a do‑while statement.[/pedantic mode]
I have found a while on line 70, however, so I was obviously not awake earlier. Sorry.

You need to go through the code and work out what your arithmetic is doing. Why have you got the two if statements nested in the loop? How does your loop calculate the throws for n dice? Why have you got DiceNo and diceNO2 local variables? Your code is so obscure and confusing that I suspect you need to delete the loop completely and start again. And as JKR said, convert that all to object‑oriented code.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Report post to moderator
Campbell Ritchie wrote:
J. Kevin Robbins wrote: . . . There is a while statement on line 25, . . . .
[pedantic mode]That isn't a while statement, but the end of a do loop. Well, officiallly a do‑while statement.[/pedantic mode]

I stand corrected. I didn't look close enough, I just saw the "while" with a semi-colon at the end and my poor brain registered "error"! My only (poor) excuse is that is I rarely use do-while loops. I try to avoid them. As I just now demonstrated, I find them somewhat confusing compared to "for" loops and I feel they are best avoided whenever possible.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Report post to moderator
J. Kevin Robbins wrote: . . . I didn't look close enough, . . .
Nor did I, since I completely missed the while at line 70.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Report post to moderator
I think we're both running low on coffee today...
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Report post to moderator
Not a drop of coffee has touched my lips all day. Nor beer
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Report post to moderator
Campbell Ritchie wrote:Not a drop of coffee has touched my lips all day. Nor beer


Well, that explains it then.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Report post to moderator
Closing because there is more of an answer here.
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!