2016-12-15 104 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

相關問題