✅ C# Academy Math Game Issues
so I've decided to start going through the projects of The C# Academy and the first one was a math game. Everything works, and I passed all their tests, however, when playing the game, I often get errors of the game attempting to divide by zero, and when I select the Medium or Hard difficulty, it gives the same number like
173/173
for the equation. Their requirements were The division should result in integers only and dividends should go from 0 to 100
. I can't figure out how to fix this. Thanks37 Replies
Well, on easy you generate numbers between 0 and 99... so a 0 will be generated every now and then
And you can't divide by 0, so that's why
The lower bound for a
random.Next()
is inclusive and the upper bound is exclusive
So, for example, the possible numbers that random.Next(0, 5)
can generate are 0
, 1
, 2
, 3
, 4
I changed it to be 1,99 instead of 0,99 thank you 🙂
right. I didn't think about that part.
so inclusive meaning it will include 1, but exclusive meaning it won't include 99? so change 99 to 100
Exactly
ok cool. so I've made that change and I currently have
so I need to change the 199 and 299 to the even hundred 200 and 300
Yep
so running the program again with those changes, I'm still getting the same number for num1 and num2 like
179/179
instead of something like 135/125
the result has to be integers only, and I know that there's more division options with numbers than just those that result in 1 for an optionRight, how often does it happen that you get two of the same number?
every single question
Well that's certainly weird
this is the division game function
It really shouldn't be generating same results
Wait, does it happen with medium difficulty as well?
yes
I know why it happens
watch it be something simple and I feel dumb 😂
The only possible way a division can happen without a rest, or without decimal spaces, is when both numbers are the same
It's true for ranges of numbers above 100, within that 100
So for 100-200 range, 200-300, etc
so do I need to make the ranges like 100-400 and 400-800?
Let's take the smallest number in 100-199 range. It's 100
The only number that can be divided by 100 in that range is... 100
I'm with you so far
So yes, you would either need to increase the ranges, or allow decimal spaces in the results
I'll have to increase the ranges because they want integers only in the results
I also have another question. I was trying to get it to where the question would show the bigger number / smaller number, but trying to do that kept resulting in a 0 divisor
Alternatively, make the answer be a decimal number, but allow the answer to be within a certain range
So the result is 78.7, and it should accept 79 as the answer
For example
won't converting the double to an int cause the rounding up/down to happen automatically?
Depends whether you ceil it, floor it, or round it
But yeah
so this should be how it's written then, yes?
and that will allow me to keep my current ranges?
Yep
Then again, worth testing what the actual results will be
Entirely possible they will all round to either 0 or 1
it's telling me that this is an ambiguous invocation but won't tell me what the actual problem is
Seems the only possible results will be 0, 1, or 2
I've tried
and I have tried
and it's not liking anything I'm doing
I think if you want to increase difficulty you should just be increasing the upper bound, but not the lower bound
So 0-100 for easy, 0-200 for medium, 0-300 for hard
That way you're going to get the least amount of issues
Or, well, lower bound being 1
ok I changed the ranges. That's not a bad idea
but I'm still having problems figuring out the cast types
here's the updated ranges
Now you can keep your integer division, the check and all
No need to fiddle with decimals now
Yeah, this works
ok I put the while-loop back in. running a test
no double numbers now 🙂 thank you!!!
Nice
thank you again for your help!