使用 Console.WriteLine() 创建列格式文本

Creating column formatted text with Console.WriteLine()

我正在使用“Console.WriteLine()”向控制台打印随机 "card"。

我正在尝试打印由两部分组成的一行。该行的第一部分显示您所抽牌的 long-name。第二部分显示花色图标和数字:

有没有办法以 neater/uniform 的方式显示该行的第二部分?是这样的:

出现问题是因为我的行的第一部分根据面值和花色改变大小。这是我用来打印卡片的代码:

Console.Write("Your card is a{0} " & textValue & " of " & suit, IIf(textValue = "Ace", "n", ""))
Console.Write("     " & suitIcon & " " & textValue)
Console.WriteLine()

我也试过以下方法:

 Console.Write("Your card is a{0} " & textValue & " of " & suit, IIf(textValue = "Ace", "n", ""))
 Dim string2 As String = (suitIcon & " " & textValue)
 Dim padAmount As Integer = 50 - (suit.Length + textValue.Length)
 Console.Write(string2.PadLeft(padAmount, " "c))
 Console.WriteLine()

将文本显示为:

我看不到你粘贴的输出内容,但是像这样的东西呢?

Module Module1

Dim lstCard As New List(Of String)
Dim lstSuit As New List(Of String)

Sub Main()

    lstCard.Add("Ace")
    lstCard.Add("King")
    lstCard.Add("Queen")
    lstCard.Add("10")
    lstCard.Add("9")

    lstSuit.Add("Spades")
    lstSuit.Add("Hearts")
    lstSuit.Add("Diamonds")
    lstSuit.Add("Clubs")

    For i As Int32 = 0 To lstSuit.Count - 1
        For j As Int32 = 0 To lstCard.Count - 1
            Console.WriteLine("Your card is {0} {1} of {2}.", IIf(lstCard(j) = "Ace", "an", "a").ToString.PadLeft(2), _
                              lstCard(j).PadLeft(5), lstSuit(i).PadLeft(8))
        Next
    Next

    Console.ReadLine()

End Sub

End Module

输出:

Your card is an   Ace of   Spades.
Your card is  a  King of   Spades.
Your card is  a Queen of   Spades.
Your card is  a    10 of   Spades.
Your card is  a     9 of   Spades.
Your card is an   Ace of   Hearts.
Your card is  a  King of   Hearts.
Your card is  a Queen of   Hearts.
Your card is  a    10 of   Hearts.
Your card is  a     9 of   Hearts.
Your card is an   Ace of Diamonds.
Your card is  a  King of Diamonds.
Your card is  a Queen of Diamonds.
Your card is  a    10 of Diamonds.
Your card is  a     9 of Diamonds.
Your card is an   Ace of    Clubs.
Your card is  a  King of    Clubs.
Your card is  a Queen of    Clubs.
Your card is  a    10 of    Clubs.
Your card is  a     9 of    Clubs.

在@Capellan 的帮助下,我找到了解决问题的方法。我还需要考虑字符串第二部分的长度。

我曾经按照以下代码这样做:

        Console.Write("Your card is a{0} " & textValue & " of " & suit, IIf(textValue = "Ace", "n", ""))
    Dim string2 As String = (suitIcon & " " & textValue)
    Dim padAmount As Integer = (25 - (suit.Length + textValue.Length)) + suitIcon.Length + textValue.Length
    If textValue = "Ace" Then
        padAmount -= 1
    End If
    Console.Write(string2.PadLeft(padAmount, " "c))
    Console.WriteLine()

这可能仍然不是最好的方法,所以请随时提交答案。它确实产生了以下内容:

您可以使用格式化参数来控制输出的填充。 -2 表示左对齐并填充到 2 个字符,5 表示右对齐并填充到 5 个字符:

Dim textValue = "Queen"
Dim suit = "Spades"
Dim article As String = IF(textValue = "Ace", "an", "a")

Console.WriteLine("Your card is {0,-2} {1,5} of {2}", article, textValue, suit)

textValue = "Ace"
suit = "Hearts"
article = IF(textValue = "Ace", "an", "a")

Console.WriteLine("Your card is {0,-2} {1,5} of {2}", article, textValue, suit)

textValue = "7"
suit = "Diamonds"
article = IF(textValue = "Ace", "an", "a")

Console.WriteLine("Your card is {0,-2} {1,5} of {2}", article, textValue, suit)

结果:

Your card is a  Queen of Spades
Your card is an   Ace of Hearts
Your card is a      7 of Diamonds