1

我正在制作一个非常简单的 Hangman 游戏并使用 2 个文件。Program.cs 和 WordList.cs。

菜单是:

  1. 添加单词

  2. 显示单词列表

  3. 出口

我想知道如何让控制台中写入的单词进入单词列表。因此,如果我选择菜单项 1,我应该最多可以输入 5 个单词并让它们进入单词列表。真的希望有人可以提供帮助,因为我有点失落。需要我说 C# 的初学者:) 我还没有弄清楚程序如何搜索每个字母,但首先处理这个问题......

这是program.cs中的代码

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

class Hangman
{
    static void Main()
    {
        Console.ForegroundColor = ConsoleColor.Red;
        Console.Title = "C# Hangman";
        Console.WriteLine("Welcome To C# Hangman!");

        //MENU

        char MenuChoice;       

        Console.Write("\n\t1) Add words");
        Console.Write("\n\t2) Show list of words");
        Console.Write("\n\t3) Play");
        Console.Write("\n\t4) Quit\n\n");

        Console.Write("\n\tChoose 1-4: ");        //Choose meny item
        MenuChoice = Convert.ToChar(Console.ReadLine());

            switch (MenuChoice)
            {
                case '1':

                    break;
                case '2':
                    WordList showing = new WordList();
                    showing.ListOfWords();
                    Console.Write("\n\tList of words\n\n");


                    break;


                case '3':   //Running game

                    int guesses;
                    Console.Write("\n\tHow many faults can you have: ");
                    guesses = Convert.ToInt32(Console.ReadLine());
                    Console.Write("\n\tAwesome, let´s play!\n");


                    String input;
                    bool wrong;
                    int NumberOfTries = 0;


                    do
                    {
                        Console.WriteLine("\n\n\tWrong guesses: " + NumberOfTries + " / " + guesses + "\n");
                        Console.WriteLine("\n\tGuessed letters:\n");
                        Console.WriteLine("\n\tWord:\n");
                        Console.Write("\n\n\tGuess letter: ");
                        input = Console.ReadLine();
                        Console.Write("\n\n\t ");

                        wrong = !input.Equals("t") &&
                              !input.Equals("e") &&
                              !input.Equals("s") &&
                              !input.Equals("t"); 
                        if (wrong)
                        {
                            NumberOfTries++;
                            Console.WriteLine("\n\tWrong letter " + "Try again!");
                        }
                        if (wrong && (NumberOfTries > guesses - 1))
                        {
                            Console.WriteLine("\n\tYou have failed " + guesses + ". End of game!\n");
                            break;
                        }

                    } while (wrong);
                    if (!wrong)
                        Console.WriteLine("\n\tWhohoo!");

                    break;

                case '4':

                    Console.WriteLine("\n\tEnd game?\n\n");
                    break;
            }

    }

}

这是 WordList.cs 中的代码

using System;
using System.Collections.Generic;

class WordList
{
    public void ListOfWords()
    {

        List<string> words = new List<string>(); // List

        words.Add("test");         // Contains: test
        words.Add("dog");          // Contains: test, dog
        words.Insert(1, "shit"); // Contains: test, shit, dog

        words.Sort();
        foreach (string word in words) // Display for verification
        {
            Console.WriteLine(word);

        }

}
}
4

4 回答 4

3

你可以使用Console.ReadLine()

        string word = "";
        while (word != null && !word.Equals("DONE"))
        {
            word = Console.ReadLine();
            wordList.Add(word);
        }
于 2013-04-19T12:01:25.013 回答
3

像这样扩展您的应用程序,将您的显示声明移到您的开关之外

var showing = new WordList();
switch (MenuChoice)
        {
            case '1':
                showing.AddWord(Console.ReadLine())
                break;
            case '2':
                showing = new WordList();
                showing.ListOfWords();
                Console.Write("\n\tList of words\n\n");

并扩展您的 Wordlist 以保留您的单词并添加添加新单词的方法

class WordList
{
   private words = new List<string>();
   'keep the constructor but move declaration

   public void AddWord(string word)
   {

    words.Add(word);
   }

事实上,通过一些重构,您可以继续删除类单词列表,并将列表保留在 Program.cs 中,但它确实可以使用更多作为重构

我会尝试修改你的代码(现在没有编译器,所以不要责怪任何小的语法问题(通常使用 VB.net)

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

class Hangman
{
    static void Main()
    {
        Console.ForegroundColor = ConsoleColor.Red;
        Console.Title = "C# Hangman";
        Console.WriteLine("Welcome To C# Hangman!");

        //MENU

        char MenuChoice;       

        Console.Write("\n\t1) Add words");
        Console.Write("\n\t2) Show list of words");
        Console.Write("\n\t3) Play");
        Console.Write("\n\t4) Quit\n\n");

        Console.Write("\n\tChoose 1-4: ");        //Choose meny item
        MenuChoice = Convert.ToChar(Console.ReadLine());
        WordList showing = new WordList();
            switch (MenuChoice)
            {
                case '1':
                    var input = Console.ReadLine();
                    showing.AddWord(input);
                    break;
                case '2':

                    showing.ListOfWords();
                    Console.Write("\n\tList of words\n\n");


                    break;


                case '3':   //Running game

                    int guesses;
                    Console.Write("\n\tHow many faults can you have: ");
                    guesses = Convert.ToInt32(Console.ReadLine());
                    Console.Write("\n\tAwesome, let´s play!\n");


                    String input;
                    bool wrong;
                    int NumberOfTries = 0;


                    do
                    {
                        Console.WriteLine("\n\n\tWrong guesses: " + NumberOfTries + " / " + guesses + "\n");
                        Console.WriteLine("\n\tGuessed letters:\n");
                        Console.WriteLine("\n\tWord:\n");
                        Console.Write("\n\n\tGuess letter: ");
                        input = Console.ReadLine();
                        Console.Write("\n\n\t ");

                        wrong = !input.Equals("t") &&
                              !input.Equals("e") &&
                              !input.Equals("s") &&
                              !input.Equals("t"); 
                        if (wrong)
                        {
                            NumberOfTries++;
                            Console.WriteLine("\n\tWrong letter " + "Try again!");
                        }
                        if (wrong && (NumberOfTries > guesses - 1))
                        {
                            Console.WriteLine("\n\tYou have failed " + guesses + ". End of game!\n");
                            break;
                        }

                    } while (wrong);
                    if (!wrong)
                        Console.WriteLine("\n\tWhohoo!");

                    break;

                case '4':

                    Console.WriteLine("\n\tEnd game?\n\n");
                    break;
            }

    }

}

这是 WordList.cs 中的代码

using System;
using System.Collections.Generic;

class WordList
{
    private List<string> words = new List<string>();

    public void ListOfWords()
    {
        words.Add("test");         // Contains: test
        words.Add("dog");          // Contains: test, dog
        words.Insert(1, "shit"); // Contains: test, shit, dog

        words.Sort();
        foreach (string word in words) // Display for verification
        {
            Console.WriteLine(word);

        }

    }

    public void AddWord(string value){
        words.Add(value);
      }
}
于 2013-04-19T12:05:56.990 回答
2

你有一个单词集合。

List<string> myList = new List<string>();

你从控制台读到这个词

var inputString = Console.ReadLine();

如果你喜欢,你可以修剪它

inputString = inputString.Trim();  // that'll remove the spaces on the front/back of the string

然后您可以将其添加到列表中

myList.Add(inputString);
于 2013-04-19T12:08:15.577 回答
1

由于单词由空格分隔,因此您可以让用户输入他们的单词列表,如下所示:
these are four words,您可以轻松阅读

string input = Console.ReadLine();
// input == "these are four words"

创建列表现在非常简单

string[] words1 = input.Split(new char[] { ' ' }, // Splits the words by space
                             StringSplitOptions.RemoveEmptyEntries);
// words1 = { "these", "are", "four", "words" }

如果你绝对需要 a List<string>,你只需要.ToList()在最后添加:

List<string> words2 = input.Split(new char[] { ' ' },
                                 StringSplitOptions.RemoveEmptyEntries).ToList();
于 2013-04-19T12:12:19.890 回答