• Post Reply Bookmark Topic Watch Topic
  • New Topic

first win not being counted  RSS feed

 
Jeremy Olsen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My first win is not being counted. Thanks for the help.

 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are outputting the old score and incrementing afterwards, which is why it always shows the old score.

BTW, you might want to read the following:

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html

 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy Olsen wrote:


You don't actually have an empty else statement. The else on line 28 actually contains a single if-else statement - all of lines 30 through 64 (I think i have that right).

Is that what you wanted? Or did you really want an empty else statement (which means you could just leave off the else entirely).
 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A better way to format the if/else if code would be:

This makes it clear that the elses aren't empty at all.
 
Jeremy Olsen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it!!

 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to look at the pre-increment operator.

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html

 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy,

I'm glad to see you worked out the problem with your while loop condition. However, your "fix" for the score problem, while it works, is what's called a kludge.

A better fix would be to remove all the System.out.println statements that display the current score from all the if-else statements and put just one similar statement after the whole if-else-if series. This will make it unnecessary to do the +1 on a player's score just to get the result you need, which frankly is not a good solution. It will also eliminate a lot of duplication in your code.

Basically, you cut line 27 and paste it back in at line 61, then fix it so that it also has "to" between the player's scores. You won't have to adjust the winning player's score by one just to display the correct values if you put the statement here. The missing "to" on line 27 is a typical problem with duplicated code. Duplicated code is bad for maintainability and consistency.

Then you can delete lines 32, 37, 43, 48, 53, & 58.

After you do this your code will be cleaner and more logical.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!