C#变量UserInput找不到定义


double UserInput;
string y, z;
double OunceToGram, PoundToOunce, PoundToKilogram, PintToLitre, InchToCenti, MilesToInch;

OunceToGram = UserInput * 28.0; //(error is here, it cannot find UserInput)
PoundToOunce = UserInput * 16.0;
PoundToKilogram = UserInput * 0.454;
PintToLitre = UserInput * 0.568;
InchToCenti = UserInput * 2.5;
MilesToInch = UserInput * 63360.0;
int i = 0;
while (i < UserInput)
{
    Console.WriteLine("");
    Console.WriteLine("Please enter a unit to convert, type a num <1 to cancel");
    UserInput = Convert.ToDouble(Console.ReadLine());

您可以解决您的问题,即不立即将用户输入转换为变量UserInput,并检查用户是否键入常规字母来停止输入

double UserInput = 0.0;  // <- You need to initialize before using it ...
.....
string stopInput = "N";
while (stopInput != "Q"))
{
   Console.WriteLine("");
   Console.WriteLine("Please enter a unit to convert, type 'Q' to cancel");
   stopInput = Console.ReadLine();
   if(stopInput == "Q")
      break;
   UserInput = Convert.ToDouble(stopInput);
   ....
}

最新更新