Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

first win not being counted  RSS feed

 
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.

 
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

 
lowercase baba
Bartender
Posts: 12627
50
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).
 
Sheriff
Posts: 5036
138
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

 
Sheriff
Posts: 12199
199
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!