2011-10-05 48 views
-3

我写一个程序,允许用户输入一个比萨饼的直径和程序计算的比萨将有多少片给你。这是我的代码到目前为止。控制台应用程序比萨

//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(); 

我该如何四舍五入输出以及如何在输出writeline中引用比萨直径的直径?

+0

什么是程序怎么了?它是否编译,运行? – MarkJ

+1

什么是错的?而且,切片的量不依赖于比萨饼的直径。 – TJHeuvel

+1

不,它不会编译。 msg错误表示“不能隐式地将类型'double'转换为'int'。存在明确的转换(你是否缺少一个转换?) –

回答

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
  • 作为@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?)

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

+0

好的,谢谢你的建议 –

0

其他人已经ointed了一些你的编码错误的。你也有逻辑错误。仔细看看这两条线:

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

你在这里做什么?你究竟在做什么?

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

diameter = 2 * radius 

area = pi * radius^2 

现在回去,并在你的代码是做再看看。

+0

“面积= Math.PI * Math.Pow(半径,2); Slices =(int)(Area/SliceSize);' 我想拿比萨饼的直径除以2,这会给我半径,我怎么编码呢? –

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();