寻找更好的方法来输出我的字符串数组

时间:2017-07-14 15:12:53

标签: c# arrays console

我是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]);


 }

3 个答案:

答案 0 :(得分:2)

我建议重新调整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());
    }
}

答案 1 :(得分:2)

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

render(){
return(
  <List containerStyle={{borderTopWidth: 0, borderBottomWidth: 0}}>
    <FlatList
      data={this.state.data}
      renderItem={({item})=>(
        <ListItem
        roundAvatar
        title={'${item.name.first} ${item.name.last}'}
        subtitle={item.email}
        avatar={{uri: item.picture.thumbnail}}
        containerStyle={{borderBottomWidth: 0}}
      />
    )}
    keyExtractor={item=>item.email}
    ItemSeparatorComponent={this.renderSeparator}
    ListHeaderComponent={this.renderHeader}
    />
  </List>
);
}

答案 2 :(得分:0)

您应该创建一个具有所需属性的对象(Person),如名称,某些东西,另一个,并使用ToString方法打印它,或者根据需要将所有字符串放在List或Array中,并使用String.Join < / p>