C#变量UserInput找不到定义
C# the variable UserInput cannot find the definition
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);
....
}
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);
....
}