using System; using System.Collections.Generic; using System.Linq; using System.
ID: 3655217 • Letter: U
Question
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int miles; int gallons; miles = 0; gallons = 0; int mpg; mpg = 0; int totalMiles; int totalGallons; totalMiles = 0; totalGallons = 0; int tripCounter; tripCounter = 0; int ompg; ompg = 0; { Console.Write("Enter miles in this tank:"); Convert.ToInt32(Console.ReadLine()); Console.Write("Enter gallons in this tank:"); Convert.ToInt32(Console.ReadLine()); while (miles != -1) { mpg = miles / gallons; totalMiles = totalMiles + miles; totalGallons = totalGallons + gallons; tripCounter = tripCounter + 1; Console.Write(" Miles per Gallons: "); Convert.ToInt32(Console.ReadLine()); if (tripCounter > 0) { ompg = totalMiles / totalGallons; Console.WriteLine("Write overall miles per gallons:"); Convert.ToInt32(Console.ReadLine()); } else { Console.WriteLine("No trips taken:"); Console.ReadLine(); } } } } } }Explanation / Answer
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int miles; int gallons; miles = 0; gallons = 0; int mpg; mpg = 0; int totalMiles; int totalGallons; totalMiles = 0; totalGallons = 0; int tripCounter; tripCounter = 0; int ompg; ompg = 0; while (miles != -1) { Console.Write("Enter miles in this tank: "); miles=Convert.ToInt32(Console.ReadLine()); if (miles != -1) { Console.Write("Enter gallons in this tank: "); gallons = Convert.ToInt32(Console.ReadLine()); ompg = miles / gallons; totalMiles = totalMiles + miles; totalGallons = totalGallons + gallons; tripCounter = tripCounter + 1; Console.WriteLine("Miles per Gallons: {0}", ompg); if (tripCounter > 0) { mpg = totalMiles / totalGallons; Console.WriteLine("overall miles per gallons: {0}", mpg); } } }//end while miles!=-1 Console.WriteLine("No trips taken. ");//right after exiting while loop Console.ReadLine(); } } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.