2016-04-13 23 views
0

我正試圖寫一個簡單的控制檯遊戲應用程序&我遇到了一些idk時刻。好的,所以在代碼中,我試圖得到一個Y/N輸入,並且如果是Y則開始遊戲,或者如果是N則退出。我很想聽聽一些各種解決方案&對於編碼示例有獎金< 3每次開始或播放遊戲時都會指定隨機門號。如何在循環中詢問是/否答案?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Diagnostics; 
using System.Threading; 

namespace Desicions 
// Typo I know. 
{ 
    class Program 
    { 
    static void Main(string[] args) 
     { 

     bool WrongInput = true; 

     while (WrongInput) 
     { 
      Console.Clear(); 
      Console.WriteLine("Would you perfer what's behind door 1, 2, or 3?"); 
      string userValue = Console.ReadLine(); 
      if (userValue == "1") 
      { 
       Console.WriteLine("You won a new car!"); 
       WrongInput = false; 
      } 
      else if (userValue == "2") 
      { 
       Console.WriteLine("You won a boat!"); 
       WrongInput = false; 
      } 
      else if (userValue == "3") 
      { 
       Console.WriteLine("Aww, you did not win this time please play again."); 
       WrongInput = false; 
      } 
      else 
      { 
       Console.WriteLine("Please choose door number 1, 2, or 3. Press Enter to return to game."); 
       Console.ReadLine(); 
      } 
     } 
     Thread.Sleep(2000); 
     Console.WriteLine(); 
     Console.WriteLine("Would you like to play again? (Y/N)"); 

     while ((Console.ReadLine() == "Y") || (Console.ReadLine() == "y")) 
      //Should I use this instead? 
      //while String.Equals(Console.ReadLine(), "y", StringComparison.CurrentCultureIgnoreCase); 
      //if so, why? 
     { 
      //What goes here for (Y)/(y) input to return into top of while (WrongInput) loop? 
      Console.WriteLine("Currently in Dev [:"); 
     } 
     while ((Console.ReadLine() == "N") || (Console.ReadLine() == "n")) 
     { 
      //I would like for a (N)/(n) input to WriteLine("Exiting game..."); Thread.Sleep(2000); then exit. Solutions plz por favor? 
     } 


     //BONUS c# <3 for code sample for setting doors numbers randomly! 

     Console.ReadLine(); 

    } 
} 

}

+0

看我如何選擇一個隨機門編輯:d – Aybe

回答

1

這裏有一個強大的有點來解決這個問題的辦法:

using System; 

namespace ConsoleApplication1 
{ 
    internal class Program 
    { 
     private static void Main(string[] args) 
     { 
      while (true) 
      { 
       Console.WriteLine("Choose 1, 2, 3 or X to exit."); 

       // read input 
       var s = Console.ReadLine(); 

       // shall we exit ? 
       if (s != null && s.Trim().Equals("X", StringComparison.InvariantCultureIgnoreCase)) 
       { 
        Console.WriteLine("Bye bye !"); 
        break; 
       } 

       // user picked an activity 
       int result; 
       if (!int.TryParse(s, out result)) continue; // failed to read input 


       // at this point, do something interesting 
       Console.WriteLine("You selected : " + result); 
      } 
     } 
    } 
} 

正如你所看到的,

  • 都不會有問題用戶是否以大寫或小寫類型,與領先/尾部空格 - >處理方法
  • 您將與用戶選擇的某個數字進行比較VS a string(更好)
  • 等...

去,並提高對:)

編輯:選擇一個隨機門

const int maxDoors = 3; 
var doors = new Dictionary<int, string> 
{ 
    {0, "Door 1"}, 
    {1, "Door 2"}, 
    {2, "Door 3"} 
}; 
var random = new Random(); 
while (true) 
{ 
    // integrate this inside the previous example loop 

    var door = random.Next(maxDoors); 
    var doorName = doors[door]; 
    Console.WriteLine("You have chosen the door: " + doorName); 
} 
+0

在考慮如何使用這個實現之後,這個例子回答了Y/N問題,而忽略了上/下問題的方式比我認爲可能的更好。 Aybe,我剛剛學到了一些新東西,謝謝。 –

+0

樂意幫忙:D – Aybe

+0

Aybe,你只是一個喜悅。我只是回到SO&看到了這個編輯&立即有它的價值!我需要得到我的排名,所以我可以+1你 –

0

您將大大受益簡單地做ToLower將惠及到Console.ReadLine()()(或ToUpper的()),忽略任何情況下的問題。

至於你現在有什麼,通過說(Console.ReadLine() == "foo" || Console.ReadLine() == "bar"),你調用命令來接受用戶輸入兩次,這意味着用戶需要輸入他們是否想在再次進入while循環之前再次播放兩次。

至於你的隨機門號碼,請查看System.Random,並在遊戲開始時將它分配給一個變量。 https://msdn.microsoft.com/en-us/library/system.random(v=vs.110).aspx

此外,而不是調用一個空Console.WriteLine();,只需在"\n"添加到您的字符串創建一個換行符。

最後,它是「更喜歡」,而不是「更好」。

+0

我明白如何將輸入力上下限的樣品,並感謝您的解釋我的錯誤上調用輸入兩次,我想知道這件事......而且這個參考文獻太多了<3,我有一些閱讀要做。至於'\ n',我做了一些研究,因爲我在Windows環境下應該明確地使用'\ r \ n'或者使用'Environment.NewLine'來支持所有的操作系​​統環境? –

0

我看到這個在上面的問題,我可能會步過我自己的極限。目前正在學習Java。只是想幫忙,對不起如果我沒用。

但是難道你不能使用do while循環,並且最終要求用戶輸入。如果Y /是,如果N /沒有,則繼續執行do while循環的檢查。

雞從頭開始:

do 
{ 
//Game code 
//ask for input 
} while input == Y | input == y 
相關問題