2017-03-17 14 views
-2

教師がパラメータ駆動型関数内に関数を格納し、スイッチをif文で置き換えるように求められました。私はこれをどうやってやろうとしているのか分かりません。どんな助けもありがとうございます。C#ハンママンゲームでパラメータ関数を使用するにはどうすればいいですか?

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に頼るのではなく、あなたが自分で作業するのを見たいと思っていたことは絶対に確信しています。将来その機能を持たない言語を使用するときはどうしますか?何もわからないので、何もありません。実際に仕事をすることを学ぶ方が、他者の仕事のために功績を残すのではなく、より良いことです。あなたが得るために支払っている程度を稼ぐ。 –

答えて

1

ハイライト "クイックアクション。" enter image description here

変更を適用した後に機能の名前を変更してください。 enter image description here

+0

これはまさに私が探していたものだと思います。私はこのことに取り組むことができました – zDynamo

関連する問題