C#我如何在我的hang子手游戏中使用参数函数

问题描述:

我的老师曾问过我将函数存储在参数驱动的函数中,并用if语句替换开关。我不知道我该如何做到这一点。任何帮助表示赞赏。C#我如何在我的hang子手游戏中使用参数函数

using System; 
using System.Collections.Generic; 
using System.Collections; 
using System.Linq; 
using System.Text; 

namespace ConsoleApplication6 
{ 
class Hangman 
{ 
    public static int lives = 5; 
    static string[] wordBank = { "study", "cat", "dress", "shoes", "lipstick" }; 
    static ArrayList wordList = new ArrayList(wordBank); 
    static void Main(string[] args) 
    { 
     Console.ForegroundColor = ConsoleColor.Green; 
     Console.Title = "C# Hangman"; 
     Console.WriteLine("Hang man!"); 
     string response = ""; 
     do 
     { 
      Console.Write("Enter Command (1. Add Words, 2. List Words , 3. Play , 4. Exit) Pick 1-4: "); 
      response = Console.ReadLine(); 

      switch (response) 
      { 
       case "1": 
        AddWord(); 
        break; 
       case "2": 
        ListWords(); 
        break; 
       case "3": 
        Play(); 
        break; 
       case "4": 
        break; 
      } 
     } while (response != "4"); 

    } 

    static void AddWord() 
    { 
     Console.Write("Enter the word to add: "); 
     String temp = Console.ReadLine(); 
     wordList.Add(temp); 
     Console.WriteLine("{0} was added to the dictionary!", temp); 
    } 
    static void ListWords() 
    { 
     foreach (Object obj in wordList) 
      Console.WriteLine("{0}", obj); 
     Console.WriteLine(); 
    } 
    static void AskLives() 

    { 
     try 
     { 
      Console.WriteLine("please enter number of lives?"); 
      lives = int.Parse(Console.ReadLine()); 
     } 
     catch 
     { 

      AskLives(); 
     } 
    } 
    static void Play() 
    { 
     Random random = new Random((int)DateTime.Now.Ticks);   
     string wordToGuess = wordList[random.Next(0, wordList.Count)].ToString(); 
     string wordToGuessUppercase = wordToGuess.ToUpper(); 
     StringBuilder displayToPlayer = new StringBuilder(wordToGuess.Length); 
     for (int i = 0; i < wordToGuess.Length; i++) 
     displayToPlayer.Append('-'); 
     List<char> correctGuesses = new List<char>(); 
     List<char> incorrectGuesses = new List<char>(); 
     bool won = false; 
     int lettersRevealed = 0; 
     string input; 
     char guess; 
     AskLives(); 
     while (!won && lives > 0) 
     { 
      Console.WriteLine("Current word: " + displayToPlayer); 
      Console.Write("Guess a letter: "); 
      input = Console.ReadLine().ToUpper(); 
      guess = input[0]; 

      if (correctGuesses.Contains(guess)) 
      { 
       Console.WriteLine("You've already tried '{0}', and it was correct!", guess); 
       continue; 
      } 
      else if (incorrectGuesses.Contains(guess)) 
      { 
       Console.WriteLine("You've already tried '{0}', and it was wrong!", guess); 
       continue; 
      } 

      if (wordToGuessUppercase.Contains(guess)) 
      { 
       correctGuesses.Add(guess); 

       for (int i = 0; i < wordToGuess.Length; i++) 
       { 
        if (wordToGuessUppercase[i] == guess) 
        { 
         displayToPlayer[i] = wordToGuess[i]; 
         lettersRevealed++; 
        } 
       } 

       if (lettersRevealed == wordToGuess.Length) 
        won = true; 
      } 
      else 
      { 
       incorrectGuesses.Add(guess); 

       Console.WriteLine("Nope, there's no '{0}' in it!", guess); 
       lives--; 
      } 

      Console.WriteLine(displayToPlayer.ToString()); 
     } 

     if (won) 
      Console.WriteLine("You won!"); 
     else 
      Console.WriteLine("You lost! It was '{0}'", wordToGuess); 

     Console.Write("Press ENTER to exit..."); 
     Console.ReadLine(); 
    } 
} 

}

+1

如果您无法开始分配任务,请向您的导师寻求帮助。他们正在获得报酬以向您提供完成所需的教育,而且看起来他们还没有这样做。 –

+0

这实际上是我完成任务的最后一步。但是,下面的人已经解决了我的问题。我认为我只是误解了如何创建“NewMethod” – zDynamo

+0

虽然这个答案可能已经为你做了工作,但我知道这是你的老师在你完成作业时的意图。我非常确定讲师希望你自己能够做到这一点,而不是依赖IDE来为你做。在将来使用不具备该功能的语言时,您会做什么?没什么,因为你不知道如何。学会真正做好这项工作,而不是仅仅因为别人的工作而得到好处就更好了。赚取你付出的学位。 –

突出显示开关块,然后选择 “快速操作”。 enter image description here

应用更改后重命名该函数。 enter image description here

+0

我想这正是我正在寻找的东西,我能够解决这个问题,谢谢你 – zDynamo