格式化多行数据的控制台输出

时间:2017-11-23 05:51:54

标签: c# console-application

我不太确定如何在技术上说出我想要做的事情,所以标题可能有些模糊。

简而言之,我正在尝试将数据输出到控制台中,以便单独格式化为多行,但整体仍然作为“单行”运行。

例如,我正在尝试将“99A”和“42B”写入控制台,使其显示为:

94
92
AB

但我能做的只是:

9
9
A
4
2
B

所以我可以正确格式化我的数据,但不能以我想要的方式将它写入Write或WriteLine。我最终会把它写成一个文件但是为了弄清楚控制台应该这样做。

对此有任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

以下是RoToRaCode Review上类似问题的Java答案的C#转换:

using System;

class MainClass {
  public static void Main (string[] args) {
    printVertical("99A 42B");
  }

  public static void printVertical(string sentence) {
    string[] words = sentence.Split(' ');

    bool allWordsEnded;
    int row = 0;

    do {
      allWordsEnded = true;

      char[] output = new char[words.Length * 2];
      int column = 0;
      foreach(String word in words) {
        char c;
        if (row < word.Length) {
          c = word[row];
          allWordsEnded = false;
        } else {
          c = ' ';
        }
        output[column++] = c;
      }
      if (!allWordsEnded) {
        Console.WriteLine(output);
      }
      row++;
    }
    while (!allWordsEnded);
  }
}

<强>输出:

94
92
AB