The compiler isn't smart enough to know that at least one of your if
blocks will be executed. Therefore, it doesn't see that variables like annualRate
will be assigned no matter what. Here's how you can make the compiler understand:
if (creditPlan == "0")
{
// ...
}
else if (creditPlan == "1")
{
// ...
}
else if (creditPlan == "2")
{
// ...
}
else
{
// ...
}
The compiler knows that with an if/else block, one of the blocks is guaranteed to be executed, and therefore if you're assigning the variable in all of the blocks, it won't give the compiler error.
By the way, you can also use a switch
statement instead of if
s to maybe make your code cleaner.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…