❔ Issues with for and foreach
Im working on a school project and im having some issues with for and foreach, this is my project: User inputs 10 numbers (in a array). after that 5 random numbers will be printed. example: user inputs 1,2,3,4,5,6,7,8,9,10. The random numbers are: 1,11,13,15,2. If the random numbers matches with ANY of the numbers user input. That will be printed. so in this example it should print: You guessed 2 numbers of the generated numbers (1) and (2). But in my code when the user matches the generated numbers it doesn't show.. Im maybe missing something in my code.
This is the code: (some prints are in swedish but that's irrelevant.) i linked the code
2 Replies
what you're doing is adding the random number to the array at index
i
and then checking every one of the random numbers against the guess at index i
, so if your guesses are: 1,2,3,4,5 ... then 1 will only be printed if the first random number is 1, 2 will be printed if the first two random numbers are 2, 3 will be printed if the first 3 random numbers are 3, etc.. guesses 6-10 are completely ignored and if the random number generator generates 1 as the fourth random number, it doesn't check the first guess again.
you need to instead make the array of random numbers and then check each of those numbers against each of the guesses.Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.