gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:54:11 25 4
gpt4 key购买 nike

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

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com