编写的if语句如下,语句中的I在后续编程中需要使用,怎样将I的值传递到if语句外面呢?
if(sinA1 > 0 && tanl0 > 0)
{
double I = Math.Abs(Math.Atan(tanl0) * Math.PI / 180);
}
else if (sinA1 > 0 && tanl0 < 0)
{
double I = Math.PI - Math.Abs(Math.Atan(tanl0) * Math.PI / 180);
}
else if (sinA1 < 0 && tanl0 < 0)
{
double I = -Math.Abs(Math.Atan(tanl0) * Math.PI / 180);
}
else if (sinA1 < 0 && tanl0 > 0)
{
double I = Math.Abs(Math.Atan(tanl0) * Math.PI / 180) - Math.PI;
}
任何语言都有变量作用域,你这种写法是在if中定义的变量是不能被外面使用的。
可以在if外面定义,在if中操作该变量。
把 I 定义为全局变量就行
double I =0;
if(sinA1 > 0 && tanl0 > 0)
{
I = Math.Abs(Math.Atan(tanl0) * Math.PI / 180);
}
else if (sinA1 > 0 && tanl0 < 0)
{
dll= Math.PI - Math.Abs(Math.Atan(tanl0) * Math.PI / 180);
}
else if (sinA1 < 0 && tanl0 < 0)
{
l = -Math.Abs(Math.Atan(tanl0) * Math.PI / 180);
}
else if (sinA1 < 0 && tanl0 > 0)
{
I = Math.Abs(Math.Atan(tanl0) * Math.PI / 180) - Math.PI;
}
请收下,全局变量l