【问题标题】:Looking for a better way to output my string array [closed]寻找一种更好的方法来输出我的字符串数组[关闭]
【发布时间】:2017-12-19 18:08:25
【问题描述】:

我是 C# 的初学者,正在创建一个小 MadLibs 控制台游戏以供练习。 一切正常,现在我正在努力让它变得更好,代码更简洁等。 刚刚从单个字符串切换到用户输入和故事输出的数组,但我认为必须有一种更有效的方法来完成这两者。

播放器类

namespace MadLib
{
static class Player
{
    public static string Input (string part)
    {
        string name;
        Console.WriteLine("Please enter a {0}: ", part);
        name = Console.ReadLine();
        return name;

        }

    }

}

故事代码块

static class Story
{
    static public void Spooky()
    {
         string[] part = new string[10];

        //ask player for words

        part[0] = Player.Input("noun");

        part[1] = Player.Input("adjective");

        part[2] = Player.Input("adjective");

        part[3] = Player.Input("adjective");

        part[4] = Player.Input("occupation");

        part[5] = Player.Input("occupation");

        part[6] = Player.Input("occupation");

        part[7] = Player.Input("adjective");

        part[8] = Player.Input("noun");

        part[9] = Player.Input("noun");

        //output finished story

        Console.WriteLine("They all agreed that it was a huge {0}, {1}, {2}, and {3}."
            +" I have cross-examined these men, one of them a hard-headed "
                + "{4}, one a {5}, and one a moorland {6}"
                + ", who all tell the same story of this "
                + "{7} {8}, exactly corresponding to the {9} of the legend."
                , part[0], part[1], part[2], part[3], part[4], part[5]
                , part[6], part[7], part[8], part[9]);


 }

【问题讨论】:

  • 看起来您想要使用对象而不是数组。 stackoverflow.com/questions/14747095/best-way-to-create-object
  • 另一件事:为什么要让播放器类静态?你显然有一个真正的玩家,所以你最终可以做Player myPlayer = new Player(); 并从那个实例中工作。和你的故事一样。不是您需要,我刚刚发现最好不要将所有内容都设为静态。
  • 我什至不确定我是否会称它为 Person。一个更好的名字可能是像 MadLibSentence。然后 MadLibSentence 的一个实例可以有它自己的字符串集合(名词、形容词等)。 MadLibSentence 可以有一个名为 GetInput 的方法,该方法循环遍历字符串集合以获取每个字符串的输入。

标签: c# arrays console


【解决方案1】:

我建议重新使用 Player 类并将其重命名为 MadLibSentence。这将允许您提前在静态类中创建 MadLibSentence 对象,以便所有部分和句子都被定义。 MadLibSentence 知道如何获取其句子的输入以及如何显示该句子已完成输入。

class Program
{
    static void Main(string[] args)
    {
        MadLibs.Legend.Start();
        MadLibs.Another.Start();

        // to keep the console open
        Console.ReadKey(true);
    }
}

public static class MadLibs
{
    public static MadLibSentence Legend = new MadLibSentence(
        new List<string>()
        {
            "noun", "adjective", "adjective", "adjective",
            "occupation", "occupation", "occupation",
            "adjective", "noun", "noun"
        },
        "They all agreed that it was a huge {0}, {1}, {2}, and {3}."
            + " I have cross-examined these men, one of them a hard-headed "
            + "{4}, one a {5}, and one a moorland {6}"
            + ", who all tell the same story of this "
            + "{7} {8}, exactly corresponding to the {9} of the legend.");

    public static MadLibSentence Another = new MadLibSentence(
        new List<string>()
        {
            "noun", "adjective", "adjective"
        },
        "This is a {0} mad lib. I don't think it is {1} {2}.");
}

public class MadLibSentence
{
    private List<string> _parts { get; set; }
    private string _sentence { get; set; }

    public MadLibSentence(List<string> parts, string sentence)
    {
        this._parts = parts;
        this._sentence = sentence;
    }

    private List<string> GetInput()
    {
        var input = new List<string>();
        foreach (var part in _parts)
        {
            Console.WriteLine("Please enter a {0}: ", part);
            input.Add(Console.ReadLine());
        }
        return input;
    }

    public void Start()
    {
        Console.WriteLine(_sentence, GetInput().ToArray());
    }
}

【讨论】:

    【解决方案2】:

    这是另一个版本,类似于 Blake 已经发布的版本,但不是面向对象的:

    class Program
    {
    
        static void Main(string[] args)
        {
            Console.WriteLine(Spooky());
    
            Console.WriteLine("");
            Console.Write("Press Enter to Quit...");
            Console.ReadKey();
        }
    
        static public string Spooky()
        {
            string story = "They all agreed that it was a huge {0}, {1}, {2}, and {3}."
                + " I have cross-examined these men, one of them a hard-headed "
                + "{4}, one a {5}, and one a moorland {6}"
                + ", who all tell the same story of this "
                + "{7} {8}, exactly corresponding to the {9} of the legend.";
    
            string[] prompts = {
                "noun", "adjective", "adjective", "adjective",
                "occupation", "occupation", "occupation",
                "adjective", "noun", "noun" };
    
            return MadLib(story, prompts);
        }
    
        static string MadLib(string story, string[] prompts)
        {
            List<string> answers = new List<string>();
            foreach (string prompt in prompts)
            {
                answers.Add(Input(prompt));
            }
            return String.Format(story, answers.ToArray());
        }
    
        static string Input(string part)
        {
            Console.Write("Please enter a {0}: ", part);
            return Console.ReadLine();
        }
    
    }
    

    【讨论】:

    • 我喜欢这个,因为它使它更简单(避免面向对象),以便 OP 作为初学者更好地理解。
    【解决方案3】:

    您应该创建一个具有所需属性的对象(Person),例如名称、某事、另一个,然后使用 ToString 方法打印它,或者根据需要将所有字符串放入 List 或 Array 中,然后使用 String.Join

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-19
      • 2011-08-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多