Создание текста в формате столбца с помощью Console.WriteLine()

Я печатаю случайную "карту" на консоль с помощью `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()

Что показывает текст как: введите здесь описание изображения


person Kyle Williamson    schedule 01.10.2015    source источник
comment
Что, если вы дополните строку (строки) до X символов?   -  person Capellan    schedule 01.10.2015
comment
@Capellan Спасибо за ваш ответ. Я обновил вопрос, попытавшись использовать отступы. Кажется, это не работает так, как мне хотелось бы.   -  person Kyle Williamson    schedule 01.10.2015


Ответы (3)


Я не вижу, что вы вставляете в качестве вывода, но как насчет чего-то подобного?

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.
person Capellan    schedule 01.10.2015

Вы можете использовать параметры форматирования для управления дополнением вывода. -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
person Chris Dunaway    schedule 01.10.2015

Я смог найти решение своей проблемы с помощью @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()

Это все еще может быть не лучший способ сделать это, поэтому не стесняйтесь отправлять ответ. Это произвело следующее:

введите здесь описание изображения

person Kyle Williamson    schedule 01.10.2015