I am writing a program that allows user to enter the diameter of a pizza and the program calculates how many slices the pizza will give you. Here is my code so far.
//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.Wr开发者_高级运维iteLine("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();
How do I round up the output and how to I reference the diameter of the pizza diameter in the output writeline?
- As @GeorgeDuckett mentioned, you're not setting radius, thus, you're getting an error that says
Use of unassigned local variable 'radius'
- You're attempting to divide two doubles and store the result in an int
slices = area/pizzaslice;
so you get the errorCannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)
.
The error list is your friend. Read the errors & Google them if you don't understand what they mean.
You don't set the radius
variable anywhere. You should be using decimal.Parse
to set it.
Console.WriteLine("Enter diameter of pizza:");
diasize = Console.ReadLine();
var radius = 0;
if (decimal.TryParse(diasize, out radius))
{
radius =/ 2;
}
You also need change slices = area/pizzaslice;
to slices = (int)(area/pizzaslice);
since slices is an integer and the result of area/pizzaslice
is a double.
Others have already ointed out some of your coding errors. You also have logic errors. Have a careful look at these two lines:
radius = Math.Pow(radius,2)*Math.PI;
radius /= 2;
What are you trying to do here? What are you actually doing?
Remember the formulas for the diameter and area of a circle:
diameter = 2 * radius
area = pi * radius^2
Now go back and look again at what your code is doing.
//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();
精彩评论