• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't figure out how to approach this program  RSS feed

 
Johan Mena
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I've been a lurker for quite a while but today I finally decided to create an account because I'm having a little bit of trouble with (what I think is) a simple java problem.

The problem goes as follows: There's a 5 digit code. When the user enters a guess at the code, the program returns two values: the number of digits in the guess that are in the correct position and the sum of those digits. For example, if the secret code is 53840, and the user guesses 83241, the digits 3 and 4 are in the correct position. Thus, the program should respond with 2 and 7. Allow the user to guess a fixed number of times.

This is what I have so far:



The final loop is the one I'm having trouble with. Here's a sample output:


Guess the 5 digit code now:
You have 10 more attempts to guess the code.
40000
Number of digits in the correct position: 2
Sum of those digits: 4
You have 9 more attempts to guess the code.
49710
Number of digits in the correct position: 3
Sum of those digits: 8
You have 8 more attempts to guess the code.
45210
Number of digits in the correct position: 4
Sum of those digits: 12
You have 7 more attempts to guess the code.


As you can see, if I guess with a different number that includes some of the digits of secretCode, sum and correctPosition will also change their values again, but that only leads to confusion. Can someone give me an idea on how to approach this?

Thanks in advance! By the way, I'm looking forward to be part of this great community, definitely not my last time here!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think at the very least, you need to reset the sum and correctPosition variables to 0 on each new guess. It looks like you just keep adding to them each time through.

 
Johan Mena
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ha. I just had a feeling it was something ridiculously simple. Thank you!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!