0

Basically, my question now is how would I compare the user generated numbers to the computer generated numbers? For example, if the computer shoots out 932 and user puts 912 I would need to be able to say there were matching numbers in the 9 and 2. I'm not sure how I would do that.

Paulie
  • 109
  • 1
  • 8
  • mod 10, loop until you run out of digits. – jdphenix Feb 14 '16 at 04:26
  • Possible duplicate of [Is there an easy way to turn an int into an array of ints of each digit?](http://stackoverflow.com/questions/829174/is-there-an-easy-way-to-turn-an-int-into-an-array-of-ints-of-each-digit) – Kote Feb 14 '16 at 04:41
  • Please don't delete your question once answered. Others may have the same question in the future. :) – Inisheer Feb 14 '16 at 05:44
  • Also, even though these values are "numbers", you can obviously deal with the comparison using strings which may be easier with the build-in .NET string methods. – Inisheer Feb 14 '16 at 05:48
  • Well, this code if for an assignment and I wanted to keep it private. – Paulie Feb 14 '16 at 05:51
  • What if the computer "shot" out 392 and the user puts in 912. Would you still say that 9 and 2 were matching? Or just the 2? – Enigmativity Feb 14 '16 at 05:57
  • I would say that the 2 would be a "strike" and the 9 would be a "ball". I'd need to compare that as well. The 9 is matching but counted as something else. – Paulie Feb 14 '16 at 06:01

2 Answers2

1

Here's a basic overview of what you'll need to do.

Note that the modulus operator (%) returns the remainder of a division operation. So, for example,

6 % 4 == 2

This is useful to you, because

x % 10

will equal the digit in the one's place.

Run this through a loop dividing the input by 10 each iteration and you'll have what you need.

jdphenix
  • 15,022
  • 3
  • 41
  • 74
  • So for the ten's and hundred's place it would be x%10%10 (for the ten's) and x%10%10%10 for the hundred's place? – Paulie Feb 14 '16 at 04:43
  • 1
    Not quite, think about what that'll do. You need to do *something* to the input number to make the result of `% 10` interesting again. – jdphenix Feb 14 '16 at 04:45
  • Ah, got it. Divide by 10 for the ten's place and divide by 100 for the hundred's place before %10. – Paulie Feb 14 '16 at 04:59
  • Exactly. And if you're using it in a loop, you can do something like `x /= 10; y = x % 10`. This way, it will be easier to change if you ever wanted to go from 3 digits to maybe 100 :) – AustinWBryan Feb 14 '16 at 05:00
0

With below code, you will get integer array into intArray from user input

string num3Digit = Console.ReadLine();
var intArray = num3Digit.Select(x=>Convert.ToInt32(x.ToString()));
Sateesh Pagolu
  • 9,282
  • 2
  • 30
  • 48