好的,这是我在这里的第一篇文章,所以请放轻松! ^。^ 我对C#和一般的编程都很陌生,我正试图通过实验室为我的大学介绍编程课程。
实验室的目的如下:
我在试图找出为什么我的最终“其他”声明不起作用时遇到了特别的麻烦。它与我在之前的实验室中完全相同,所以代码对于我需要的内容没有错,但是当条目不在12-36“之间时,我的if-else语句应该跳下来显示我的“错误输入”消息说明它不在范围内。我的其余代码可以工作,但我无法弄清楚为什么当条目不正确时它不会显示我的else语句!
我的代码目前正在进行一些微小的更改,以便尝试让它发挥作用:
do
{
if (double.TryParse(Console.ReadLine(), out pizzaDiameter) == false) //
{
Console.Write("\n\tENTRY ERROR\nDiameter must be a numerical number!");
Console.Write("\n\nPlease enter the diameter of your pizza (0 to end program): ");
Console.ReadLine();
}
else
{
if (pizzaDiameter >= 12 && pizzaDiameter <= 36)
{
pizzaRadius = pizzaDiameter / 2; //Calculating the radius of the pizza by dividing diameter by 2
wholePizzaArea = Math.Pow(pizzaRadius, 2) * Math.PI; //Determining the whole area of the pizza by multiplying radius^2 by pi
if (pizzaDiameter > 30)
{
Console.Write("Cut in " + SMALL_PIZZA + " slices results in a slice area of " + Math.Round(wholePizzaArea / SMALL_PIZZA, 2) + "\" per slice");
Console.Write("\nCut in " + MEDIUM_PIZZA + " slices results in a slice area of " + Math.Round(wholePizzaArea / MEDIUM_PIZZA, 2) + "\" per slice");
Console.Write("\nCut in " + LARGE_PIZZA + " slices results in a slice area of " + Math.Round(wholePizzaArea / LARGE_PIZZA, 2) + "\" per slice");
Console.Write("\nCut in " + EXTRA_LARGE_PIZZA + " slices results in a slice area of " + Math.Round(wholePizzaArea / EXTRA_LARGE_PIZZA, 2) + "\" per slice");
}
else if (pizzaDiameter > 24)
{
Console.Write("Cut in " + SMALL_PIZZA + " slices results in a slice area of " + Math.Round(wholePizzaArea / SMALL_PIZZA, 2) + "\" per slice");
Console.Write("\nCut in " + MEDIUM_PIZZA + " slices results in a slice area of " + Math.Round(wholePizzaArea / MEDIUM_PIZZA, 2) + "\" per slice");
Console.Write("\nCut in " + LARGE_PIZZA + " slices results in a slice area of " + Math.Round(wholePizzaArea / LARGE_PIZZA, 2) + "\" per slice");
}
else if (pizzaDiameter > 20)
{
Console.Write("Cut in " + SMALL_PIZZA + " slices results in a slice area of " + Math.Round(wholePizzaArea / SMALL_PIZZA, 2) + "\" per slice");
Console.Write("\nCut in " + MEDIUM_PIZZA + " slices results in a slice area of " + Math.Round(wholePizzaArea / MEDIUM_PIZZA, 2) + "\" per slice");
}
else if (pizzaDiameter >= 12)
{
Console.Write("Cut in " + SMALL_PIZZA + " slices results in a slice area of " + Math.Round(wholePizzaArea / SMALL_PIZZA, 2) + "\" per slice");
}
else
{
Console.Write("\n\tENTRY ERROR\n\nPizza must have a diameter in the range of 12\" to 36\" inclusive! \n\nPlease try again.");
}
}
}
Console.Write("\n\nPlease enter the diameter of your pizza (0 to end program): ");
} while (pizzaDiameter > 0);
}
}
}
}
答案 0 :(得分:3)
看似括号中的问题
将你的最后一个改成这个,应该有所帮助:
else if (pizzaDiameter >= 12)
{
Console.Write("Cut in " + SMALL_PIZZA + " slices results in a slice area of " + Math.Round(wholePizzaArea / SMALL_PIZZA, 2) + "\" per slice");
}
}//add this bracket here
else
{
Console.Write("\n\tENTRY ERROR\n\nPizza must have a diameter in the range of 12\" to 36\" inclusive! \n\nPlease try again.");
//} remove this one
}