Question
I have written a code in c# that asks a user to enter their name, destination, miles driven and gallons used and returns a mpg
I have written a code in c# that asks a user to enter their name, destination, miles driven and gallons used and returns a mpg value. I need to have to have runtime errors shown with one of these conditions with each new run. 1. A missing input value, when prompted just hit enter. 2. Received a string, expected a double. 3. Received a double expected an int. 4. received a double expected an string. 5. when asked for gallons used enter 0. The issue is I cant seem to get these errors to appear or if they do they all seem similar?
Console.WriteLine("Please enter name"); string Name = Console.ReadLine(); Console.WriteLine("Please enter destination"); string Destination = Console.ReadLine(); Console.WriteLine("Enter the number of miles driven (whole numbers please): "); int milesDriven = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the number of gallons used (whole numbers please): "); float gallonsUsed = float.Parse(Console.ReadLine());
float mpg = milesDriven / gallonsUsed; if (mpg > 30) { Console.WriteLine($"{Name}, the trip to {Destination} was very economical."); Console.WriteLine($"You drove {milesDriven} miles using {gallonsUsed} gallons."); Console.WriteLine($"The fuel efficiency of this trip was {mpg} miles per gallon."); Console.WriteLine($"With a MPG of over 30 the trip was good in cost efficiency."); } else if (mpg >=21 && mpg <= 30) { Console.WriteLine($"{Name}, the trip to {Destination} was economical."); Console.WriteLine($"You drove {milesDriven} miles using {gallonsUsed} gallons."); Console.WriteLine($"The fuel efficiency of this trip was {mpg} miles per gallon."); Console.WriteLine($"With a MPG close to 30 the trip was average in cost efficiency."); } else if (mpg <= 20) { Console.WriteLine($"{Name}, the trip to {Destination} was not economical."); Console.WriteLine($"You drove {milesDriven} miles using {gallonsUsed} gallons."); Console.WriteLine($"The fuel efficiency of this trip was {mpg} miles per gallon."); Console.WriteLine($"With a MPG of under 20 the trip was poor in cost efficiency."); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started