- Engineering
- Computer Science
- so i did an assignment for c and i was...
Question: so i did an assignment for c and i was...
Question details
So I did an assignment for c# and I was wondering if there is a better way to get it done? here are my directions also my code is at the bottom.
In this assignment you're going to Prompt the user for a double for the radius of a circle. You will convert their input to a double and calculate the area based on Pi * radius * radius or Pi r squared. Use Double.TryParse to attempt the conversion from the String value in the readline to the out variable (double) that you'll use for the calculation.
You can find examples of Double.TryParse here: https://msdn.microsoft.com/en-us/library/994c0zb1(v=vs.110).aspx
Keep prompting them for a valid number until they enter one. Then calculate and display the area of their circle.
You will then ask them if they'd like to calculate another (y/n). If they type anything but n and y then keep asking them until they enter y or n. You can use a readline for this or I recommend a readkey.
Y will cause the program to run again, n will cause the program to end. Code for uppercase and lowercase y's and n's.
My Program:
using System;
namespace Assignment2
{
class Program
{
static void
Main(string[] args)
{
string
input;
double
bob;
Console.WriteLine("Please
enter a radius:");
input
= Console.ReadLine();
Double.TryParse(input,
out bob);
bob
= 3.14 * (bob) * (bob);
Console.WriteLine("You
inputted: {0}", bob);
Console.WriteLine("Would
you like to enter another?(y/n): ");
input
= Console.ReadLine();
char
innput = char.Parse(input);
if
(innput == 'y' || innput == 'Y')
{
Main(args);
}
else
if (innput == 'n' || innput == 'N')
{
Console.ReadKey();
}
}
}
}
Solution by an expert tutor
