如何“预见”字符串输入?

时间:2019-12-22 11:05:15

标签: c# .net eof

string inputString;
while ((inputString = Console.In.ReadLine()) != null)
{
magic...

for (var i = 1; i < bestHands.Count; i++)
    Console.Out.Write(
    table.DisplayCards(bestHands[i - 1].Item2.Cards())
    + (bestHands[i].Item1 == bestHands[i - 1].Item1 ? "=" : " ")
    + (i == bestHands.Count - 1 ? table.DisplayCards(bestHands[i].Item2.Cards()) : ""));

Console.Out.WriteLine();
}

我们有以下代码,问题是:我们可以以某种方式检查是否有更多输入,以便在程序执行结束时不必移至新行吗?

2 个答案:

答案 0 :(得分:2)

反转思路:每次打印之前都要换行-第一次除外。

string inputString;
while ((inputString = Console.In.ReadLine()) != null)
{
    //magic...

    bool needNewLine = false;
    for (var i = 1; i < bestHands.Count; i++)
    {
        if (needNewLine)
            Console.Out.WriteLine();
        else
            needNewLine = true;

        Console.Out.Write(
            table.DisplayCards(bestHands[i - 1].Item2.Cards())
          + (bestHands[i].Item1 == bestHands[i - 1].Item1 ? "=" : " ")
          + (i == bestHands.Count - 1 ? table.DisplayCards(bestHands[i].Item2.Cards()) : ""));
    }
}

答案 1 :(得分:0)

<Window.Resource>
    <DataTemplate TargetType="local:TypeA">
        <TextBlock Text="{Binding ValueA}"/>
    </DataTemplate>
    <DataTemplate TargetType="local:TypeB">
        <TextBlock Text="{Binding ValueB}"/>
    </DataTemplate>
</Window.Resource>
<ContentControl>
    <!-- The content is a TypeA object, will apply the TypeA DataTemplate -->
    <!-- displayed 100 -->
    <local:TypeA ValueA="100"/>
</ContentControl>