【问题标题】:Creating column formatted text with Console.WriteLine()使用 Console.WriteLine() 创建列格式文本
【发布时间】:2015-10-01 13:04:02
【问题描述】:

我正在使用 `Console.WriteLine()' 将随机“卡片”打印到控制台。

我正在尝试用两部分打印一行。该行的第一部分显示了您绘制的卡片的长名称。第二部分显示西装图标和号码:

有没有办法以更整洁/统一的方式显示该行的第二部分?像这样的东西:

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

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()

将文本显示为:

【问题讨论】:

  • 如果将字符串填充到 X 个字符的长度会怎样?
  • @Capellan 感谢您的回复。我已经尝试使用填充更新了这个问题。它似乎不像我想要的那样工作。

标签: vb.net format command-line-interface multiple-columns console.writeline


【解决方案1】:

我看不到您粘贴的输出内容,但是这样的内容呢?

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.

【讨论】:

    【解决方案2】:

    您可以使用格式化参数来控制输出的填充。 -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
    

    【讨论】:

      【解决方案3】:

      在@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()
      

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

      【讨论】:

        猜你喜欢
        • 2022-07-12
        • 2023-03-03
        • 2011-05-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-29
        • 1970-01-01
        相关资源
        最近更新 更多