2011-10-05 80 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();