什么是使用未分配的局部变量错误?

时间:2015-10-27 15:50:36

标签: c#

 int num1 , num2;
 bool equel ;
 equel =((num1 % 10 == num2 % 10 || num1 % 10 == num2 / 10) && (num1 / 10 == num2 / 10 || num1 / 10 == num2 % 10));

     Console.WriteLine("enter two numbers");
     Console.WriteLine("enter the first two digir number");
     num1 = int.Parse(Console.ReadLine());
     Console.WriteLine("enter the secound to digit numbers");
     num2=int.Parse(Console.ReadLine());

        if(equel)
            Console.WriteLine("the digits of the two numbers are equels");
        else
            Console.WriteLine("the digits of the two numbers are not equels");

错误1使用未分配的本地变量'num1'D:\ project visual studios \ ConsoleApplication6 \ ConsoleApplication6 \ Program.cs 15 29 ConsoleApplication6

2 个答案:

答案 0 :(得分:1)

您正在使用'num1'来计算'equel'而不为其指定值。移动

equel =((num1 % 10 == num2 % 10 || num1 % 10 == num2 / 10) && (num1 / 10 == num2 / 10 || num1 / 10 == num2 % 10));

到你读完num1和num2

之后的值

答案 1 :(得分:0)

尝试以这种方式更改代码:

int num1 , num2;
bool equel ;

Console.WriteLine("enter two numbers");
Console.WriteLine("enter the first two digir number");
num1 = int.Parse(Console.ReadLine());
Console.WriteLine("enter the secound to digit numbers");
num2=int.Parse(Console.ReadLine());

equel =((num1 % 10 == num2 % 10 || num1 % 10 == num2 / 10) && (num1 / 10 == num2 / 10 || num1 / 10 == num2 % 10));


if(equel)
{
   Console.WriteLine("the digits of the two numbers are equels");
}
else
{
   Console.WriteLine("the digits of the two numbers are not equels");
}