c# - 从控制台 C# 添加列表项

标签 c# list

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

菜单是:

  • 加词
  • 显示单词列表
  • 播放
  • 退出

  • 我想知道如何让控制台中写入的单词进入单词列表。因此,如果我选择菜单项 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);
    
            }
    
    }
    }
    

    最佳答案

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

    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);
          }
    }
    

    关于c# - 从控制台 C# 添加列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16104109/

    相关文章:

    c++ - 在初始化列表c++中初始化类数组

    c# - 用不同类型的对象填充列表

    c# - 在 winforms 应用程序上绘制相反(反向)颜色

    python - 如何在循环中增加一个值

    html - 带编号的 CSS 自定义列表样式类型

    Hibernate:一对多关系中集合的空索引列

    java - 比较大型列表并提取缺失数据

    c# - 如何判断资源是否不受管理?

    c# - 如何从 WPF 窗口创建缩略图并将其转换为 bytes[] 以便我可以保留它?

    c# - 错误 : Cannot find 'freetype6.dll'