2014-02-19 35 views
0
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace BehindClosedDoors 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string userValue; 
      Console.WriteLine("Hello, Welcome to Behind Closed Doors"); 
      Console.WriteLine("A fun and exciting game"); 
      Console.WriteLine("Roll Two dice and the add the value's together and enter that number"); 
      userValue = Console.ReadLine(); 

      do while (userValue == "2") 
      { 
       Console.WriteLine("Kiss"); 
       Console.WriteLine("Now roll another number"); 
       Console.ReadLine(); 
      } 

      while ((userValue = Console.ReadLine()) == "3"); 
      { 
       Console.WriteLine("Give"); 
       Console.WriteLine("Now roll another number"); 
       Console.ReadLine(); 
      } 

      while ((userValue = Console.ReadLine()) == "4") 
      { 
       Console.WriteLine("Foot Rub"); 
       Console.WriteLine("Now roll another number"); 
       Console.ReadLine(); 
      } 

      while (userValue == "2" + "3" + "4") 
      { 
       // 
      } 
      // else if (userValue == "5") 
     } 
    } 
} 

我想使它所以當我運行此代碼,我可以輸入爲2 3或4的很多次,我想並獲得無控制檯關閉在我的文字。現在我可以選擇兩個,然後在第三個關閉的控制檯上。問題與while語句關閉我的控制檯

+0

你都知道,在第一個while循環,你剛纔讀的控制檯,但從來沒有給它分配? – Styxxy

+0

我建議閱讀關於while循環來了解它是如何工作的以及如何使用它。 http://msdn.microsoft.com/en-us/library/2aeyhxcd.aspx – kschieck

+0

看起來就像我需要回去重讀一些我想用循環做的例子。我只學了兩個星期的學習C# – user3323014

回答

1

您正在檢查「2」+「3」+「4」,即「234」作爲字符串。

你可能要像

userValue == "2" || userValue == "3" || userValue == "4" 
+0

好吧,這使控制檯關閉我可以打任何數字,我想要多少次,因爲我想和它保持開放。如果我輸入2 3或4,我的新問題就無關緊要了。我甚至可以輸入23並獲得該文本。 – user3323014

+0

在「while」循環中2 - 永遠不會重新分配userValue。所以它總是2. – Haedrian

+0

你也真的想要這樣的循環?是否有任何理由你沒有使用ifs或switch語句(如Sergey建議的那樣) – Haedrian

3

我認爲你需要帶開關單迴路:

do 
{ 
    userValue = Console.ReadLine(); 

    switch(userValue) 
    { 
     case "2": //.. 
     case "3": //.. 
     case "4": //..    
    } 

}while (userValue == "2" || userValue == "3" || userValue == "4")