我有错误说明使用未分配的局部变量'n' [英] I'm having Error stating Use of unassigned local variable 'n'

查看:59
本文介绍了我有错误说明使用未分配的局部变量'n'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码: -



I've following code:-

int p, t, n;
decimal r;
double a;

p = Convert.ToInt32(txtLAmount.Text);
r = Convert.ToDecimal(lblroibuss);
r = r / 100;


if (Convert.ToDecimal(txtLyear.SelectedItem.Value) <= 1)
{
    t = 1;
}

else
{
    t = Convert.ToInt32(txtLyear.SelectedItem.Value);
}

if(lblprocfees.Text=="Annually")
{n=1;}
else if (lblprocfees.Text == "Quarterly")
{ n = 4; }
else if (lblprocfees.Text == "Monthly")
{ n = 12;
}

double temp = Convert.ToDouble(1 + (r / n));
double temp1 = n * t;
a = p * Math.Pow(temp, temp1);

    txtLAmmount.Text = Math.Round(a,2).ToString();







但它给了我错误使用未分配的局部变量'n'。无法理解为什么会发生这种情况,即使我已经宣布并在代码中指定'n'条件。



请帮助! :(




But it's giving me the error Use of unassigned local variable 'n'. Can't understand why this is happening even when I've declared and put on the condition in the code assigning 'n'.

PLease HELP!! :(

推荐答案

局部变量(在你的方法中声明的变量)不是自我初始化的,因为类成员是自我初始化的。所以



公共类A

{

int a; //这将初始化a = 0

}



public void MyMethod()

{

int a; //这不会给变量任何值

}



你必须用你方法中的值初始化变量,给它一个默认值。此外,小心除零除外在你的代码中,因为你在计算中使用'n'作为分母。



问候,

Sujay C。
Local variables (variables declared inside your method) are not self initialized where as class members are self initialized. So

public class A
{
int a; //This will initialize a = 0
}

public void MyMethod()
{
int a; //This will not give variable a any value
}

You must initialize your variable with a value within your method, give it a default value. Furthermore, be careful of divide by zero exception in your code, as you are using 'n' as denominator in your calculation.

Regards,
Sujay C.


这篇关于我有错误说明使用未分配的局部变量'n'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆