使用未分配的局部变量

时间:2013-10-14 04:00:00

标签: c#

我必须建立一个程序,它取一个披萨的直径,找到它可以容纳多少切片和每个切片的面积,披萨,它用常量定义披萨大小,但是得到一个错误“int numberOfSlice“它是说使用未分配的局部变量,甚至认为我分配了if语句。

class Program
{
    static void Main(string[] args)
    {
        //declarations of Constans

        const int SMALL_MIN = 12; 
        const int SMALL_MED = 16;
        const int MED_LARGE = 24;
        const int LARGE_XLARGE = 30;
        const int XL_MAX = 36;
        const int SMALL_SLICE = 8;
        const int MED_SLICE = 12;
        const int LARGE_SLICE = 16;
        const int XL_SLICES = 24;
        //declarations of varable
        double pizzaDiameter;
        int numberOfSlices = 0;
        double sliceArea;
        double radius; 
        string userInput = " ";


        Console.WriteLine("Please enter the diameter of your pizza:"); // tell user to input diameter 
        userInput = Console.ReadLine(); // gets userinput
        double.TryParse(userInput, out pizzaDiameter); // see if userinput is vaild


        if (pizzaDiameter >= SMALL_MIN && pizzaDiameter <= XL_MAX) // if in range will continue
        {

            // all the ranges for the pizzas 
            if (pizzaDiameter >= SMALL_MIN && pizzaDiameter < SMALL_MED)
            {
                numberOfSlices = (SMALL_SLICE);
            }

            else if (pizzaDiameter >= SMALL_MED && pizzaDiameter < MED_LARGE)
            {
                numberOfSlices = (MED_SLICE);
            }

            else if (pizzaDiameter >= MED_SLICE && pizzaDiameter < LARGE_XLARGE)
            {
                numberOfSlices = (LARGE_SLICE);
            }

            else if (pizzaDiameter >= LARGE_XLARGE && pizzaDiameter <= XL_MAX)

            {
                numberOfSlices = (XL_SLICES);
            }


            radius = pizzaDiameter / 2; // divides pizzaDiameter to get radius 


            sliceArea = Math.PI * Math.Pow(radius, 2) / numberOfSlices; // gets slice area

            sliceArea = Math.Round(sliceArea, 2); // rounds to 2 places

            // output of resluts 
            Console.WriteLine("\nA diameter of " + pizzaDiameter + " will yield " + numberOfSlices + " slices.");
            Console.WriteLine("\nEach slice will have an area of " + sliceArea + "\".");

            Console.WriteLine("\nPress any key to exit..."); // tells user to end program
            Console.ReadKey(); // readkey to end program


        }



        else // if the diameter was not in range will display this error 
        {
            Console.WriteLine("\nEntry Error ");
            Console.WriteLine("\nPizza must have a diameter in the range of 12\" to 36\" inclusive!");
            Console.WriteLine("please try again");

            Console.WriteLine("\nPress any key to end this application...");// tells user to end program
            Console.ReadKey(); // readkey to end program

        }      






    }
}

}

4 个答案:

答案 0 :(得分:4)

错误会告诉您需要知道的一切。您的numberOfSlices变量未正确初始化。在使用它之前,必须为每个可能的代码路径分配一个值。

尝试在声明时初始化它:

int numberOfSlices = 0;

或者,您可以更仔细地构建if - 块以避免此错误:

if (pizzaDiameter <= XL_MAX || pizzaDiameter >= SMALL_MIN)
{
    if (pizzaDiameter >= SMALL_MIN || pizzaDiameter < SMALL_MED)
    {
        numberOfSlices = (SMALL_SLICE);
    }
    ...
    else  // Note, you do not need the final `if` in this block
    {
        numberOfSlices = (XL_SLICES);
    }

    radius = pizzaDiameter/2;

    sliceArea = Math.PI * Math.Pow(radius, 2);

    Console.WriteLine("\nA diameter of " + pizzaDiameter + " will yield." + numberOfSlices + " slices.");
    Console.WriteLine("\nEach slice will have an area of " + sliceArea + "\".");
    Console.WriteLine("\nPress any key to exit...");
    Console.ReadKey();
}
else
{
    Console.WriteLine(" Entry Error ");
    Console.WriteLine("Pizza must have args diameter in the range of 12\" to 36\" inclusive!");
    Console.WriteLine("please try again");
    Console.WriteLine(" /nPress any key to end this application...");
    Console.ReadKey();
}

在此代码中,numberOfSlices仅在if块内使用,保证在使用之前为其分配值。

答案 1 :(得分:2)

该变量仍在外else块中取消分配:

if (pizzaDiameter <= XL_MAX || pizzaDiameter >= SMALL_MIN)
{
}
else
{
    // unassigned here
}

答案 2 :(得分:1)

如果if (pizzaDiameter <= XL_MAX || pizzaDiameter >= SMALL_MIN)返回false,则numberOfSlices将不会被分配,下面的行将会失败。

  Console.WriteLine("\nA diameter of " + pizzaDiameter + " will yield." + numberOfSlices + " slices.");

答案 3 :(得分:1)

您收到错误是因为您仅在if块中指定了该错误 如果条件不满意怎么办?在这种情况下,它不会被初始化,从而抛出该错误。

编译器在使用任何变量之前检查每个可能的代码路径中的赋值。

您需要做的是,

int numberOfSlices=0; // Or whatever initial value you want to give
if (pizzaDiameter <= XL_MAX || pizzaDiameter >= SMALL_MIN)
{
}
else
{

}
//Then, even if you try to access it here, it won't throw any error.

注意:它不是由default value的int(即0)初始化的,因为它不是类的数据成员。