最后的Else语句在do ... while循环中不起作用。 C#

时间:2014-10-16 05:03:24

标签: c# if-statement do-while

好的,这是我在这里的第一篇文章,所以请放轻松! ^。^ 我对C#和一般的编程都很陌生,我正试图通过实验室为我的大学介绍编程课程。

实验室的目的如下:

  • 提示用户输入以英寸为单位的披萨直径
  • 披萨直径可以输入实数。确保 用户输入是使用TryParse()方法的数字
  • 当用户输入“0”表示直径时,立即结束程序而不进行任何操作 进一步处理,消息或提示
  • 检查输入的直径是否在12“至36”(含)范围内
  • 如果条目无效,则显示错误消息并重新提示输入有效条目
  • 如果条目有效,请计算披萨面积并确定最大切片数量
  • 12“直径20”的直径只能切成8片。
  • 直径大于20“且直径大于24”的直径可以切割成最多12个切片。
  • 大于24“直径30英寸的直径可以切割成最多16个切片。
  • 大于30“的直径可以切割成最多24个切片。
  • 创建一个格式化的输出,以显示披萨的所有可能切割配置列表以及每个配置的切片数量和切片区域
  • 继续提示用户输入披萨直径,仍然提供0以结束程序
  • 在每个输入直径的列表中显示结果
  • 使用Console.Clear()清除控制台每次显示新列表之前

我在试图找出为什么我的最终“其他”声明不起作用时遇到了特别的麻烦。它与我在之前的实验室中完全相同,所以代码对于我需要的内容没有错,但是当条目不在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);

            }
        }
    }
}

1 个答案:

答案 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
                    }