我测试了一个简单的计算器函数,作为我的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