2012-03-29 58 views
0

我正在研究一個C#應用程序,該應用程序應該輸入英里驅動和每加侖使用的汽油(整數),然後應用程序應計算並顯示每加侖獲得的英里數對於每輛坦克,並顯示爲每輛坦克獲得的每加侖英里數。我的問題是,標記控制方面「-1」不會停止運行程序,但是,IDE啓動了一個JIT調試器,並且我得到了一個Microsoft.NET框架異常MileageDriven.exe [732]。Visual Studio C#2010練習第四版

我可以得到一些幫助,因爲我不想超越這個練習,因爲未來的課程將建立在最後一課之上。提前致謝。 :)

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

很難知道我們如何能夠在不看任何代碼或異常幫助.. – 2012-03-29 07:13:08

+0

確實,我會發布我寫的代碼。謝謝。只是一個瞬間。 – 2012-03-29 07:14:20

+0

我試圖張貼代碼,但沒有骰子。我只是在學習這個網站。裸露在一起。 – 2012-03-29 07:18:19

回答

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"); 
    }