Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML 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:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

first win not being counted

 
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
Posts: 12753
51
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: 6107
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • 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: 13549
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • 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!