2017-06-04 102 views
0

我正在使用while循環使其在用戶輸入除「Yes」或「No」之外的任何內容時,它會告訴他們輸入新的內容,但每次輸入其他內容時只是垃圾郵件「這不是一個選項」,而不是從頂端開始。有人可以向我解釋爲什麼?先謝謝你。無法獲取while循環工作

   using System; 

class CalculatorProgram 
{ 
    //varibale for do-while loop 
    private static string endAnswer; 
    public static void Main() // <----- The Entry point 
    { 
    //Variables 
    string Choice1; 
    string mathChoice; 
    decimal Num1; 
    decimal Num2; 
    decimal Answer; 
    bool userWrong = true; 

    Console.Write("Would you like to use Lane's Custom Calculator?(Yes/No): "); 
    Choice1 = Console.ReadLine(); 

    while(userWrong) 
    { 
     if (Choice1 == "Yes") 

     { 
      do 
      { 
       Console.Write("Would you like to Add, Subtract, Multiply, or Divide? (Case Sensitive): "); 
       mathChoice = Console.ReadLine(); 

       //User inputs the 2 numbers 


       //Math Choices 
       if (mathChoice == "Add") 
       { 
        Console.WriteLine("What 2 numbers would you like to use?"); 
        Console.Write("Number 1 is: "); 
        Num1 = decimal.Parse(Console.ReadLine()); 

        Console.Write("Number 2 is: "); 
        Num2 = decimal.Parse(Console.ReadLine()); 

        Answer = Num1 + Num2; 
        Console.WriteLine("Your expression is: " + Num1 + " + " + Num2 + " = " + Answer); 
       } 

       else if (mathChoice == "Subtract") 
       { 
        Console.WriteLine("What 2 numbers would you like to use?"); 
        Console.Write("Number 1 is: "); 
        Num1 = decimal.Parse(Console.ReadLine()); 

        Console.Write("Number 2 is: "); 
        Num2 = decimal.Parse(Console.ReadLine()); 
        Answer = Num1 - Num2; 
        Console.WriteLine("Your expression is: " + Num1 + " - " + Num2 + " = " + Answer); 
       } 

       else if (mathChoice == "Multiply") 
       { 
        Console.WriteLine("What 2 numbers would you like to use?"); 
        Console.Write("Number 1 is: "); 
        Num1 = decimal.Parse(Console.ReadLine()); 

        Console.Write("Number 2 is: "); 
        Num2 = decimal.Parse(Console.ReadLine()); 
        Answer = Num1 * Num2; 
        Console.WriteLine("Your expression is: " + Num1 + " X " + Num2 + " = " + Answer); 
       } 

       else if (mathChoice == "Divide") 
       { 
        Console.WriteLine("What 2 numbers would you like to use?"); 
        Console.Write("Number 1 is: "); 
        Num1 = decimal.Parse(Console.ReadLine()); 

        Console.Write("Number 2 is: "); 
        Num2 = decimal.Parse(Console.ReadLine()); 
        Answer = Num1/Num2; 
        Console.WriteLine("Your expression is: " + Num1 + "/" + Num2 + " = " + Answer); 
       } 

       else 
       { 
        Console.WriteLine("This is not an option! Shutting Down.."); 
        Console.ReadKey(); 
        Environment.Exit(0); 
       } 


       //varibale for while loop to continue if selected Yes. 
       Console.Write("Another Equation?: "); 
       endAnswer = Console.ReadLine(); 



      } while (endAnswer == "Yes"); 


      //Goodbye Message 
      Console.WriteLine("Thank you for using my program, goodbye "); 
      Console.ReadKey(); 
      Environment.Exit(0); 
      userWrong = false; 
     } 
     //If someone selects no for wanting to use my program. 
     else if (Choice1 == "No") 
     { 
      Console.WriteLine("Thank you for using my program, goodbye "); 
      Console.ReadKey(); 
      Environment.Exit(0); 
     } 
     else 
     { 
      Console.WriteLine("That is not an option"); 
      Console.ReadLine(); 


     } 

    } 





    } 
} 
+0

你沒讀過的用戶選擇第二次,你這樣做的,而循環之外,然後在循環裏,你只是打印「這是不是一種選擇「並重復。只需用你在while循環之外的同一行更新你的代碼就可以了:'Choice1 = Console.ReadLine();' – pstrjds

+0

順便說一句,歡迎來到SO。您正在學習編程並且我不希望您在提問時感到氣餒,即使我已經投票決定將此問題視爲排版錯誤的結果。當你學習編程時,你會有更多的問題,這是一個很好的論壇來解答這些問題。作爲另一個無關的方面說明,您可能希望在比較時使用[string.Equals](https://msdn.microsoft.com/en-us/library/858x0yyx(v = vs.110).aspx)方法'strings'。它允許您在比較時指定序號,區分大小寫,文化等。 – pstrjds

+0

非常感謝! – Laneciar

回答

2

該程序正在等待用戶輸入新的答案後,它打印出「這不是一個選項」,你只是從來沒有提示他們。你也絕不會存儲Choice1再根據他們對新的輸入,所以它總是會檢查您do-while循環的第一if條件用他們最初插嘴說。

要解決它,你else分支體更改爲類似這個。

//... 
else 
{ 
    Console.WriteLine("That is not an option"); 
    // reprompt the user so they know to type something in 
    Console.Write("Would you like to use Lane's Custom Calculator?(Yes/No): "); 
    // store the new choice to recheck next loop iteration 
    Choice1 = Console.ReadLine(); 
} 
+0

非常感謝你! – Laneciar

-1

可能你的問題是你沒有對輸入做出正確的驗證。 現在,您從控制檯讀取輸入並與靜態字符串進行比較,因此 「添加」與「添加」不同,並且與「添加(空間)」不同。 我建議你做一個更強大的輸入驗證:

Console.Write("Would you like to Add, Subtract, Multiply, or Divide? (Case Sensitive): "); 
mathChoice = Console.ReadLine(); 
mathChoice = mathChoice.ToUpper().Trim(); 
//User inputs the 2 numbers 

//Math Choices 
if (mathChoice == "ADD") 
{ 
    Console.WriteLine("What 2 numbers would you like to use?"); 
    .... 
} 
+0

根據提示的提示,「區分大小寫」的驗證是正確的,爲了使其不區分大小寫,最好使用接受比較類型的'string.Equals'覆蓋,比如'OrdinalIgnoreCase'或其他類似的東西。然而,重複循環的問題與從控制檯讀取的else語句中的「添加」它的驗證無關,但將結果轉儲到地板上,因此循環繼續檢查相同的錯誤值,而不是存儲最新的一個進入。 – pstrjds

+0

我知道,我爲新用戶提供了一個簡單的解決方案。如果我實現了這個功能,我將使用帶有標誌的enum.TryParse。我的回答是關於正確輸入驗證的重要性。 – Linefinc

+0

我明白你在說什麼,我正在解釋我的downvote的原因,你沒有回答實際問題。問題在於沒有讀入循環中的輸入,而不是用戶鍵入「添加」的驗證,但你聲明「可能你的問題是你沒有做出正確的驗證......」 – pstrjds