2015-10-03 103 views
-1

我遇到了我的代碼問題。我必須做出一個簡單的選擇遊戲,我需要這樣做,以便如果用戶輸入一個無效的選項,它會暫停故事,直到他們選擇有效的響應。我嘗試在一會兒(1 == 1)中包裝整個事物,然後在控制檯窗口中輸入無效響應,並且無限地打印出「那不是一個選項」。我如何解決這個問題?謝謝。忽略不正確的用戶輸入,直到收到正確的輸入

  // First choice, scene 1 if-statements 
     while (1 == 1) 
     { 
      String firstScene = Console.ReadLine(); 
      firstScene = firstScene.Trim(); 
      String firstChoice = firstScene.ToLower(); 
      Console.WriteLine(); 

      if (firstChoice == "rudely") 
      { 
       assholeFactor++; 
       Console.WriteLine(">>\"You're damn right it was! We've been working on this for years. I thought you'd be happy that all those hours at the office actually amounted to something.\" (Douche factor increased)"); 
       break; 
      } 
      if (firstChoice == "nicely") 
      { 
       niceFactor++; 
       Console.WriteLine(">>\"No, it wasn't silly. I can see where you're coming from. I suspect there will be a lot of people with those same type of questions upon the release of the first model.\" (Nice factor increased)"); 
       break; 
      } 
      if (firstChoice == "silence") 
      { 
       judgement--; 
       Console.WriteLine(">>You sip your wine and say nothing. (Judgement decreased)"); 
       break; 
      } 
      if (firstChoice != "rudely" || firstChoice != "nicely" || firstChoice != "silence") 
      { 
       Console.WriteLine("That wasn't an option."); 
       continue; 
      } 
     } 
+2

哪裏循環? – Steve

+0

while循環?我刪除了它,但它在if(firstChoice)行之前。 – crin

+1

這只是一個簡單的錯誤,你應該測試firstChoice2,而不是firstChoice。如果你沒有選擇好的變量名,那麼你會添加錯誤。 –

回答

0

必須重新讀取用戶輸入到內變量檢查它的條件之前循環:

var input = ReadLine(); 
while (true) // or some other condition 
{ 
    if (input=="...") { } ... 

    input = ReadLine(); // again 
} 

另外,我在assholeFactor++

+0

無論如何,最好讓它成爲一個do-while,因爲你需要一個循環。 – khlr

+0

好的,所以我應用了這個建議(不正確,我敢肯定),但是當我第一次輸入無效響應時,它根本沒有返回任何消息。我在同一個控制檯窗口中再次嘗試,並說「這不是一個選項」。當我試圖輸入一個有效的答覆時,它繼續說「這不是一個選項」。 – crin

+0

在逐步模式下通過您的代碼進行調試。我100%肯定你會發現你的流量有缺陷。 – SimpleVar

2

你可以這樣做loled 。基本上,你有一個標誌來控制循環。繼續它,直到你得到正確的輸入。一旦你得到正確的輸入,通過設置標誌來停止循環。

string firstChoice = "getInputFromUser...."; 
var isCorrectInput = false; 

do 
{ 
    if (firstChoice == "rudely") 
    { 
     isCorrectInput = true; //stop further loop iteration 
     assholeFactor++; 
     ..... 
    } 
    else if 
     ... //set isCorrectInput as well 
    else 
    { 
     //if input didn't match options, continue loop 
     Console.WriteLine("That wasn't an option. Enter again..."); 
     firstChoice = Console.ReadneLine(); // 
    } 
} while(!isCorrectInput); 
0

如果問題得到了正確解答,您可以使用標誌檢查每個循環。事情是這樣的:

var answered = false; 
while(!answered) // Loop until the question has been correctly anwered 
{ 
    var firstChoice = ReadLine(); 
    if (firstCoice == "rudely") 
    { 
     assholeFactor++; 
     Console.WriteLine(">>\"You're damn right it was! We've been working on this for years. I thought you'd be happy that all those hours at the office actually amounted to something.\" (Douche factor increased)"); 
     answered = true; // Question answered --> shouldn't keep asking 
    } 
    else if... 
    else 
    { 
     Console.WriteLine("That wasn't an option."); 
    } 
} 
0

你可以這樣做:

Dictionary<string,Tuple<string,Action>> Choices = new Dictionary<string, Tuple<string, Action>>(); 
    int assholeFactor = 0; 
    int niceFactor = 0; 
    int judgement  = 0; 

    Choices.Add("rudely" , new Tuple<string, Action>("You're damn right it w.."   ,() => assholeFactor++)); 
    Choices.Add("nicely" , new Tuple<string, Action>("No, it wasn't silly. I ca.."  ,() => niceFactor++ )); 
    Choices.Add("silence", new Tuple<string, Action>("You sip your wine and say nothing.",() => judgement-- )); 

    do 
    {   
     string option = Console.ReadLine(); 
     if (Choices.ContainsKey(option)) 
     { 
      Console.Out.WriteLine(Choices[option].Item1); 
      Choices[option].Item2();      
      break; 
     } 
     Console.WriteLine("That wasn't an option."); 
    } while (true);