c#console application未分配的局部变量

时间:2015-07-07 22:00:02

标签: c# visual-studio visual-studio-2012 console-application

        int ValueOne, ValueTwo, Numchar, Total;
    Console.WriteLine("This Is A Program For doing any Of the four mathematical Proccesses");
    Console.WriteLine("You can Add , substract , Divide And Multiply");
    Console.WriteLine("When Asked Please Type The Values Or The Proccesses You Want.");
    Console.WriteLine("Please Type The First Value");
    ValueOne = Convert.ToInt32((Console.ReadLine()));
    Console.WriteLine("Please Type The Second Value");
    ValueTwo = Convert.ToInt32((Console.ReadLine()));
    Console.WriteLine("Please Enter The Number Of The Proccess/Character You Want Meaning That (1 = +) , (2 = -) , (3 = *) , (4 = /)");
    Numchar = Convert.ToInt32((Console.ReadLine()));
    if (Numchar == 1)
        Total = ValueOne + ValueTwo;
    if (Numchar == 2)
        Total = ValueOne + ValueTwo;
    if (Numchar == 3)
        Total = ValueOne * ValueTwo;
    if (Numchar == 4)
        Total = ValueOne / ValueTwo;
    Console.WriteLine("The Total Is :");
    Console.WriteLine(Total);
    Console.ReadLine();

Console.WriteLine(总计); 最后显然是一个未分配的局部变量,虽然它应该从“If”线下面的Lines分配,但也注意到我是视觉工作室的新手,我刚开始参加课程,我也不认为这是重复的,因为其他帖子的修复对我来说不起作用。

1 个答案:

答案 0 :(得分:1)

假设您的代码没有产生错误,并编译并运行。如果NumChar不是1234,您认为会发生什么?

您的代码永远不会到达if中的任何语句,也绝不会将初始值分配给Total。由于这是完全可能的,因为你的程序(并且通常无法检查 - 参见暂停问题),错误是有根据的,也是必要的。