2016-12-15 106 views
0

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

我不知道为什么这样做,这应该只是罚款。 请帮助我,谢谢! :)

+3

下一次这种类型的问题将被关闭。 “寻求调试帮助的问题(”为什么不是这个代码工作?“)必须包含所需的行为,特定的问题或错误以及在问题本身中重现问题所需的最短代码。没有明确问题陈述的问题对其他读者无用' – Reniuz

+0

明白了,下次不会发生:) – davidgpilot

回答

-2

你可以改变你的循环条件是这样的:

while (calc != "sumCalc" || calc != "multiCalc") 
1

试图改变你的代码:的

while (calc != "sumCalc" && calc != "multiCalc") 

代替:

while (calc != "sumCalc" || calc != "sumCalc") 
2

你有sumCalc两次在while的条件。如果您使用OR作为条件,即使您输入sumCalcmultiCalc,也会将||更改为&&,该循环将为真,并不断要求您再次输入。

while (calc != "sumCalc" && calc != "multiCalc") 
+0

是的对不起,我输入了它而不是multiCalc:P – davidgpilot

+2

@Reniuz'&&'是正确答案这里 – FakeCaleb

+1

@Reniuz如果你使用OR,它将是无止境的,因为循环将永远是真实的,我在发布答案之前测试了这一点。 – abdul

相关问题