我正在开发一个C#应用程序,它应该为每一罐气体输入里程驱动和加仑使用(整数),然后应用程序应计算并显示每个容器获得的每加仑英里数并显示每英里组合里程数加仑为所有坦克获得。我的问题是,标记控制方面“-1”不会阻止程序运行,但IDE会启动一个JIT调试器,我得到一个Microsoft.NET框架异常MileageDriven.exe [732]。
我可以得到一些帮助,因为我不想超越这个练习,因为未来的课程建立在最后一课的基础之上。提前致谢。 :)
using System;
public class MileagedrivenTest
{
public static void Main(string[] args)
{
Mileage driverMileage = new Mileage("Driver log");
driverMileage.DisplayMessage();
driverMileage.DetermineMileagePerGallon();
} // end main
} // end class MileageDrivenTest
// Fig. 1.1: MileageDriven.cs
// Mileage class that solves mileage per gallon problem
// using sentinel-controlled repetition
using System;
public class Mileage
{
public string Gallons { get; set; }
public Mileage (string name)
{
Gallons = name;
}
public void DisplayMessage()
{
Console.WriteLine("Welcome to the mileage log for\n{0}\n", Gallons);
} // end method DisplayMessage
//determine the average mileage for gallon per tank of gas
public void DetermineMileagePerGallon()
{
int total = 0;
int mileage; // sum of miles driven
int gallons; // sum gallons used
double average;
int mileageCounter = 0;
Console.WriteLine("Enter mileage or -1 to quit: ");
mileage = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter gallons used: ");
gallons = Convert.ToInt32(Console.ReadLine());
while (mileage != -1)
{
mileage = total + mileage;
mileageCounter = mileageCounter + mileage;
Console.Write("Enter next miles driven or -1 to quit: ");
mileage = Convert.ToInt32(Console.ReadLine());
} // end while
if (mileageCounter != 0)
{
average = (double)total / mileageCounter;
Console.WriteLine("\nTotal of the {0} miles driven is {1} per tank is{}", mileageCounter, total);
Console.WriteLine("Average mile per gallon is {0}", average);
}
else
Console.WriteLine("No mileage entered");
}
}
答案 0 :(得分:0)
您发布的代码会在此行引发异常:
Console.WriteLine("\nTotal of the {0} miles driven is {1} per tank is{}", mileageCounter, total);
问题是额外的{}
。如果你删除代码将运行但我不认为它解决了你试图解决的问题。我根据我对问题的解释修改了DetermineMileagePerGallon() - 希望它能让你更接近解决方案:
//determine the average mileage for gallon per tank of gas
public void DetermineMileagePerGallon()
{
int totalTankfuls = 0;
int inputMileage;
int totalMileage = 0; // sum of miles driven
int totalGallons = 0; // sum gallons used
Console.WriteLine("Enter mileage or -1 to quit: ");
while (!Int32.TryParse(Console.ReadLine(), out inputMileage)) ;
while (inputMileage != -1)
{
totalTankfuls++;
if (inputMileage > 0)
totalMileage += inputMileage;
Console.WriteLine("Enter gallons used: ");
int inputGallons;
while (!Int32.TryParse(Console.ReadLine(), out inputGallons)) ;
if (inputGallons > 0)
totalGallons += inputGallons;
Console.WriteLine("Enter mileage or -1 to quit: ");
while (!Int32.TryParse(Console.ReadLine(), out inputMileage)) ;
} // end while
if (totalTankfuls > 0)
{
double averageMpg = (double)totalMileage / totalGallons;
double averageMileagePerTankful = (double)totalMileage / totalTankfuls;
Console.WriteLine("Total of {0} miles driven on {1} tanks of fuel; average is {2} miles per tankful", totalMileage, totalTankfuls, averageMileagePerTankful);
Console.WriteLine("Average mile per gallon across all tankfuls is {0}", averageMpg);
}
else
Console.WriteLine("No mileage entered");
}