0

Level Points Code Coach C# Attempt

I have tried to solve the question, but I have some errors in my code, what does it mean by 'Use of unassigned local variable 'pointsCount' " and "Not all code paths result in an output"? Here is my code: https://code.sololearn.com/cwx27vKyxA7J/?ref=app

1st Apr 2021, 9:55 AM
Query
Query - avatar
4 odpowiedzi
+ 1
Initialize pointcount with 0 or whatever value. You cannot use uninitialized value in C#, unless it's guaranteed to initialized before using. In your code though it seems that it is guaranteed since the loop is guaranteed to run according to the logic in the code. C# compiler is not smart enough to aware that. It only knows that there is a for loop that can initialize pointcount, but it has no idea if the loop runs. Since it doesn't know if it runs or how many times it runs, if it doesn't run, the return value will be uninitialized. "Well, that's illegal". So error occurred. Same thing can also happen in if and switch.
1st Apr 2021, 11:24 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 1
Your code might not return a value if levels is lower than 1. It could happen, but again, C# doesn't know that it's impossible to get levels below 1. You either define an else and return something, or just define a return statement.
2nd Apr 2021, 2:07 AM
你知道規則,我也是
你知道規則,我也是 - avatar
0
Thanks! What about not all code paths result in an output?
2nd Apr 2021, 1:22 AM
Query
Query - avatar
0
Thank you so much!
2nd Apr 2021, 5:00 AM
Query
Query - avatar