The task is a lot more complex than what you thought it was (based on your code snippet). It's actually a game called Masterminds, but instead of using colors you use numbers, which adds more difficulty to the game as there are a lot more options. On the other hand it makes it makes it a lot easier when you show which numbers are positioned wrongfully and which are correct.
The reason why you're code isn't properly working is because you're comparing values instead of positions, which is something that can't be done with integers. 81 is larger than 15, but it doesn't say that the one is a correct number but is in the wrong position.
While you're comparing numbers, you should in fact be comparing strings. You need to read in a string of numbers, and compare each and every character in it to the random number, also stored in a string.
Here's basically what the code should do to compare both strings:
- loop over the read-string (let's say the current character is read_i)
- loop over the random (which is now fixed) number (let's say the current character is rand_i)
- check if read_i equals rand_i
- if so:
- check if the loop-pointers are equal
- if so:
- if not:
- if not:
Note that my above pseudo-code will show weird result when you enter a digit twice and it has a match: Suppose 12341 is supposed to match 98176, than the 1 will show up twice in red, while in fact only one 1 is correct (I don't know exactly how it works in mastermind, so you'll have to ask).
Feel free to ask for more information, I'd be happy to help you out.
I've made a mastermind game in Java a couple of years ago, so I have a fully working code, but in my opinion it's better that you find the solution yourself, rather than me giving it to you