繁体   English   中英

从consol C#添加列表项

[英]Add list item from consol C#

我正在制作一个非常简单的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);

        }

}
}

您可以使用Console.ReadLine()

        string word = "";
        while (word != null && !word.Equals("DONE"))
        {
            word = Console.ReadLine();
            wordList.Add(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");

并扩展您的单词表以保留您的单词,并添加一种方法来添加新单词

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

你有很多话。

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

由于单词之间用空格隔开,因此您可以让用户输入其单词列表,如下所示:
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" }

如果您绝对需要List<string> ,则只需在末尾添加.ToList()

List<string> words2 = input.Split(new char[] { ' ' },
                                 StringSplitOptions.RemoveEmptyEntries).ToList();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM