[英]Use of unassigned local variable
我必須建立一個程序來獲取比薩餅的直徑,並找到它可以容納多少個切片以及每個切片的面積(比薩餅),它用常數定義比薩餅的大小,但是“ 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
}
}
}
}
錯誤告訴您所有您需要了解的內容。 您的numberOfSlices
變量未正確初始化。 使用它之前,必須在每個可能的代碼路徑中為其分配一個值。
嘗試在聲明時對其進行初始化:
int numberOfSlices = 0;
或者,您可以更仔細地構造if
-block以避免此錯誤:
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
塊內部if
,該塊保證在使用之前為其分配值。
該變量仍未在外部else
塊中分配:
if (pizzaDiameter <= XL_MAX || pizzaDiameter >= SMALL_MIN)
{
}
else
{
// unassigned here
}
如果if (pizzaDiameter <= XL_MAX || pizzaDiameter >= SMALL_MIN)
返回false,那么numberOfSlices
不會被分配,並且以下行將失敗。
Console.WriteLine("\nA diameter of " + pizzaDiameter + " will yield." + numberOfSlices + " slices.");
之所以得到該錯誤,是因為您僅在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.
注意:它不是由int的默認值 (即0)初始化的,因為它不是類的數據成員。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.