Here is the program in Main
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GasMileageCalculator
{
class Program
{
/***********
* this program will calculate
* miles per gallon given
* an input of mile and gallons
* ***************/
static void Main(string[] args)
{
//program variables
double miles;
double gallons;
double mpg;
bool goodMiles;
bool goodGallons;
//we use a do loop and a try parse to make sure the entry is
//in the correct format
do
{
Console.WriteLine("How many miles were traveled");
//the try parse returns a bool true or false
//if good it assigns the result to the out parameter miles
//if false it assigns 0 to miles
goodMiles = double.TryParse(Console.ReadLine(), out miles);
if (!goodMiles)
{
Console.WriteLine("Enter a valid mileage as numbers");
}//end if
} while (!goodMiles); //end do
do
{
Console.WriteLine("How many Gallons");
goodGallons = double.TryParse(Console.ReadLine(), out gallons);
if (!goodGallons)
{
Console.WriteLine("Enter a valid number for Gallons");
} //end if
} while (!goodGallons); //end do
mpg = miles / gallons;
Console.WriteLine("You got {0:F2} miles per gallon", mpg);
//Console.WriteLine("You got " + mpg.ToString("#0.00") + " miles per gallon");
//Math.Round(mpg, 2);
Console.ReadKey();
}//end main
}//end class
}//end namespace
Here is the program with methods
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GasMileageMethods
{
class Program
{
///
/// this program calculates miles but
/// breaks it into methods
/// GetMiles() prompts the user for miles
/// GetGallons() prompts the user for gallons
/// CalculateMPG() calcualtes the miles per gallon
/// Display(double mileage) displays the results
/// the mileage is passed to display as a
/// parameter from CalculateMPG()
///
///
static void Main(string[] args)
{
//load the program class into memory
Program p = new Program();
//call the Method to calculate the mpg
p.CalculateMPG();
//call the end program method
p.EndProgram();
}
///
/// this method prompts the user for miles
/// it uses a try parse and a do loop
/// to check if its a valid entry
///
/// miles
private double GetMiles()
{
double miles;
bool goodMiles;
do
{
Console.WriteLine("Enter the miles traveled");
goodMiles = double.TryParse(Console.ReadLine(), out miles);
if (!goodMiles)
{
Console.WriteLine("Enter a valid number for miles");
}//end if
} while (!goodMiles);//end while
return miles;
}//end GetMiles
///
/// This method prompts the use for Gallons
/// and uses a try parse and a do loop to check
/// for the validy of the answer
///
/// gallons
private double GetGallons()
{
double gallons;
bool goodGallons;
do
{
Console.WriteLine("Enter the Gallons used");
goodGallons = double.TryParse(Console.ReadLine(), out gallons);
if (!goodGallons)
{
Console.WriteLine("Enter a valid number for Gallons");
}//end if
} while (!goodGallons);//end while
return gallons;
}//end GetGallons
///
/// the calculate method calls the GetMiles()
/// and the GetGallons() method to get
/// the values and then calculated the miles
/// per gallon. It passes the variable storing the
/// result to the
///
private void CalculateMPG()
{
double distance = GetMiles();
double gas = GetGallons();
double mpg = distance / gas;
//double mpg=GetMiles()/GetGallons();
Display(mpg);
}//end CalculateMPG
private void Display(double mileage)
{
Console.WriteLine("You mileage is " + mileage.ToString("F2"));
}//end Display
private void EndProgram()
{
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}//
}//end class
}
