控制台应用程序披萨切片

时间:2011-10-05 14:54:23

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

我正在编写一个程序,允许用户输入披萨的直径,程序会计算披萨给你的切片数量。到目前为止,这是我的代码。

 //DECLARATION OF VARIABLES
        string Diameter = null;           //The diameter of the pizza which the user will enter
        int Slices;                       //The number of slices the user will get
        const double SliceSize = 14.125;  //The area of each slice of pizza
        double Radius;                    //The radius of the pizza
        double Area;                      //The area of the pizza


        //INPUT
        Console.WriteLine("Enter diameter of pizza:");
        Diameter = Console.ReadLine();
        double Diameter1 = Convert.ToDouble(Diameter);

        //PROCESS
        Radius = Diameter1 / 2;
        Area = Math.PI*Math.Pow(Radius,2);
        Slices = (int)(Area / SliceSize);

        //OUTPUT
        Console.WriteLine("A Diameter\" pizza will yeild {0:n0} slices", Slices);


        // END - pause the program so the user can read the output and waits for user to press any key to exit the console
        Console.WriteLine("\n\nPress any key to exit...");
        Console.ReadKey();

如何对输出进行舍入以及如何在输出写入线中引用比萨直径的直径?

4 个答案:

答案 0 :(得分:2)

  • 正如@GeorgeDuckett所提到的,你没有设置半径,因此,你收到的错误是Use of unassigned local variable 'radius'
  • 您正在尝试划分两个双打并将结果存储在int slices = area/pizzaslice;中,以便您收到错误Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)

错误列表是你的朋友。阅读错误& 谷歌他们,如果你不明白他们的意思。

答案 1 :(得分:0)

您没有在任何地方设置radius变量。您应该使用decimal.Parse进行设置。

Console.WriteLine("Enter diameter of pizza:"); 

diasize = Console.ReadLine();
var radius = 0;    
if (decimal.TryParse(diasize, out radius))
{
    radius =/ 2;
}

您还需要将slices = area/pizzaslice;更改为slices = (int)(area/pizzaslice);,因为切片是一个整数,而area/pizzaslice的结果是双倍。

答案 2 :(得分:0)

其他人已经解决了一些编码错误。您还有逻辑错误。仔细看看这两行:

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

你想在这做什么?你在做什么?

请记住圆的直径和面积的公式:

diameter = 2 * radius

area = pi * radius^2

现在回过头来再看看你的代码在做什么。

答案 3 :(得分:0)

//DECLARATION OF VARIABLES
        string Diameter = null;           //The diameter of the pizza which the user will enter
        int Slices;                       //The number of slices the user will get
        const double SliceSize = 14.125;  //The area of each slice of pizza
        double Radius;                    //The radius of the pizza
        double Area;                      //The area of the pizza


        //INPUT
        Console.Write("Enter diameter of pizza: ");
        Diameter = Console.ReadLine();
        double Diameter1 = Convert.ToDouble(Diameter);

        //PROCESS
        Radius = Diameter1 / 2;
        Area = Math.PI*Math.Pow(Radius,2);
        Slices = (int)Math.Round(Area / SliceSize);

        //OUTPUT
        Console.WriteLine("A " + Diameter + "\" pizza will yeild {0:n0} slices", Slices);


        // END - pause the program so the user can read the output and waits for user to press any key to exit the console
        Console.WriteLine("\n\nPress any key to exit...");
        Console.ReadKey();