我測試了一個簡單的計算器函數,作爲我的C#研究的一部分,並且我遇到了問題,即使我輸入正確的選擇,它也不會退出while循環。 下面是代碼:簡單的控制檯應用程序計算器功能
static void Main(string[] args)
{
string calc, inputX, inputY, inputZ;
double x, y, z;
Console.Write("Welcome to the cool calculator. Please choose between sumCalc or multiCalc: ");
calc = Console.ReadLine();
while (calc != "sumCalc" || calc != "sumCalc")
{
Console.Write("Please type in the right calculator again: ");
calc = Console.ReadLine();
}
if (calc == "sumCalc")
{
Console.WriteLine("You are now working with the sumCalc.");
//Getting user input for the variable 'x'
Console.WriteLine("Please enter a value for the first number:");
inputX = Console.ReadLine();
x = Convert.ToDouble(inputX);
//Getting user input for the variable 'y'
Console.WriteLine("Please enter a value for the second number:");
inputY = Console.ReadLine();
y = Convert.ToDouble(inputY);
//Getting user input for the variable 'z'
Console.WriteLine("Please enter a value for the third number:");
inputZ = Console.ReadLine();
z = Convert.ToDouble(inputZ);
Console.WriteLine("The result is:" + sumCalc(x, y, z));
Console.ReadKey();
}
else if (calc == "multiCalc")
{
Console.WriteLine("You are now working with the multiCalc.");
//Getting user input for the variable 'x'
Console.WriteLine("Please enter a value for the first number:");
inputX = Console.ReadLine();
x = Convert.ToDouble(inputX);
//Getting user input for the variable 'y'
Console.WriteLine("Please enter a value for the second number:");
inputY = Console.ReadLine();
y = Convert.ToDouble(inputY);
//Getting user input for the variable 'z'
Console.WriteLine("Please enter a value for the third number:");
inputZ = Console.ReadLine();
z = Convert.ToDouble(inputZ);
Console.WriteLine("The result is:" + multiCalc(x, y, z));
Console.ReadKey();
}
}
//This is the multiply calculator function
static double sumCalc(double x, double y, double z)
{
double res = x + y + z;
return res;
}
//This is the multiply calculator function
static double multiCalc(double x, double y, double z)
{
double res = x * y * z;
return res;
}
我不知道爲什麼這樣做,這應該只是罰款。 請幫助我,謝謝! :)
下一次這種類型的問題將被關閉。 「尋求調試幫助的問題(」爲什麼不是這個代碼工作?「)必須包含所需的行爲,特定的問題或錯誤以及在問題本身中重現問題所需的最短代碼。沒有明確問題陳述的問題對其他讀者無用' – Reniuz
明白了,下次不會發生:) – davidgpilot