我的程序應該計算每加侖行程里程。雖然循環沒有退出
該程序運行良好,直到我嘗試退出,當我在循環內。當用戶在三個輸入中的任何一點輸入「quit」但它只是進入下一個輸入時應該退出。即使我在所有三個輸入中都輸入了quit,它只會崩潰。
事情我已經嘗試:
- 所有輸入三個輸入
- 退出僅使用1條件的循環,而不是三個
- 把strBeginningOdometerReading.ToUpper()= 1-10000000 !
- 更改所有的||到& &
- 更改退出到一個字符串變量
using System;
namespace MPG
{
class Program
{
static void Main()
{
//Declare the variables
string strBeginningOdometerReading, strEndingOdometerReading, strNumberOfGallons;
double dblBeginningOdometerReading, dblEndingOdometerReading, dblNumberOfGallons, dblMilesPerGallon, dblMilesTravelled;
//Priming prompt & read
Console.Write("Enter the beginning odometer reading: ");
strBeginningOdometerReading = Console.ReadLine();
Console.Write("Enter the ending odometer reading: ");
strEndingOdometerReading = Console.ReadLine();
Console.Write("Enter the number of gallons purchased for six fill-ups during the trip.");
strNumberOfGallons = Console.ReadLine();
//Convert
dblBeginningOdometerReading = Convert.ToDouble(strBeginningOdometerReading);
dblEndingOdometerReading = Convert.ToDouble(strEndingOdometerReading);
dblNumberOfGallons = Convert.ToDouble(strNumberOfGallons);
//Calculations
dblMilesTravelled = dblEndingOdometerReading - dblBeginningOdometerReading;
dblMilesPerGallon = dblMilesTravelled/dblNumberOfGallons;
//Display
Console.WriteLine("Miles Travelled: \t\t\t {0:n2} miles", dblMilesTravelled);
Console.WriteLine("Number of Gallons Purchased for 6 fill-ups: {0:n2} gallons", dblNumberOfGallons);
Console.WriteLine("Miles per Gallon (MPG): \t\t\t {0:n2} mpg", dblMilesPerGallon);
//While loop
while (strBeginningOdometerReading.ToUpper() != "QUIT" || strEndingOdometerReading.ToUpper() != "QUIT" || strNumberOfGallons.ToUpper() != "QUIT")
{
Console.WriteLine("Enter QUIT at any time to exit.");
Console.Write("Enter the beginning odometer reading: ");
strBeginningOdometerReading = Console.ReadLine();
Console.Write("Enter the ending odometer reading: ");
strEndingOdometerReading = Console.ReadLine();
Console.Write("Enter the number of gallons purchased for six fill-ups during the trip: ");
strNumberOfGallons = Console.ReadLine();
//Convert
dblBeginningOdometerReading = Convert.ToDouble(strBeginningOdometerReading);
dblEndingOdometerReading = Convert.ToDouble(strEndingOdometerReading);
dblNumberOfGallons = Convert.ToDouble(strNumberOfGallons);
//Calculations
dblMilesTravelled = dblEndingOdometerReading - dblBeginningOdometerReading;
dblMilesPerGallon = dblMilesTravelled/dblNumberOfGallons;
//Display
Console.WriteLine("Miles Travelled: \t\t\t {0:n2} miles", dblMilesTravelled);
Console.WriteLine("Number of Gallons Purchased for 6 fill-ups: {0:n2} gallons", dblNumberOfGallons);
Console.WriteLine("Miles per Gallon (MPG): \t\t\t {0:n2} mpg", dblMilesPerGallon);
} // end while
} //end Main
} //end class
} //end namespace
感謝所有幫助每個人! – Ryker