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
4 ответов
+ 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.
+ 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.
0
Thanks! What about not all code paths result in an output?
0
Thank you so much!