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

时间:2015-10-01 13:04:02

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

我正在打印一张随机的"卡"使用`Console.WriteLine()'到控制台。

我正在尝试打印一个包含两部分的线条。该行的第一部分显示了您绘制的卡片的长名称。第二部分显示套装图标和数字: enter image description here

有没有办法以整洁/统一的方式显示线的第二部分?像这样的东西: enter image description here

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

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

其中显示的文字为: enter image description here

3 个答案:

答案 0 :(得分: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.

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

我曾经按照以下代码执行此操作:

            final RemoteViews fViews = views;
            final int fViewId = viewId;

            Runnable myRunnable = new Runnable() {
                @Override
                public void run() {

                    GenericRequestBuilder<Uri, InputStream, SVG, Bitmap> requestBuilder;

                    requestBuilder = Glide.with(mContext)
                            .using(Glide.buildStreamModelLoader(Uri.class, mContext), InputStream.class)
                            .from(Uri.class)
                            .as(SVG.class)
                            .transcode(new SvgBitmapTranscoder(), Bitmap.class)
                            .sourceEncoder(new StreamEncoder())
                            .cacheDecoder(new FileToStreamDecoder<SVG>(new SvgDecoder()))
                            .decoder(new SvgDecoder())
                            .placeholder(R.drawable.ic_launcher)
                            .error(R.drawable.no_icon)
                            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                            .load(Uri.parse(url));

                    requestBuilder.into(new SimpleTarget<Bitmap>() {
                                @Override
                                public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                    fViews.setImageViewBitmap(fViewId, resource);
                                }
                            });
                }
            };
            mainHandler.post(myRunnable);

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

enter image description here

相关问题