首页 > 解决方案 > C# 使用 messagebox.show 在一行中存储和显示计算的迭代

问题描述

我正在研究这个程序。我的目的是将计算的输入数据的结果存储在一个 int[] 变量中,并使用 messagebox.show 在一行中显示。

int[] data = new int[] { 65, 66, 67, 32, 100, 90 };        // I declare int[] data it contain my data that I want to work with the length change.
int[] array = new int[6];  // i declare a table length of 6
  foreach (var b in data)   // for every element in my data I want to do this operations and build my array.
    {
      array[0] = b / 200;
      array[1] = b / 79;
      array[2] = b / 27;
      array[3] = b / 19;
      array[4] = b / 21;
      array[5] = b / 3;


Console.WriteLine("{0}", string.Join(" ", array));  // this line is for console application 
// output of this line is :
/*
0 0 2 3 3 21
0 0 2 3 3 22
0 0 2 3 3 22
0 0 1 1 1 10
0 1 3 5 4 33
0 1 3 4 4 30 */
MessageBox.Show(" "+ string.Join(" ", array)); // this line is for windowsform application 
              

我的目的是在 windowsform 应用程序中使用 messagebox.show 显示我的变量。我的目标是将它们存储在一个变量中并像这样显示它们:

0 0 2 3 3 21 0 0 2 3 3 22 0 0 2 3 3 22 0 0 1 1 1 10 0 1 3 5 4 33 0 1 3 4 4 30

我真的很感激任何帮助。

亲切的问候

标签: c#

解决方案


您可以简单地将字符串加入循环中,然后在消息框中的循环之外显示它们。使用StringBuilder类追加结果。

StringBuilder sb = new StringBuilder();
for(...)
{
  ...
  ...
  sb.AppendFormat("{0} ", string.Join(" ", array).Trim())
}

MessageBox.Show(sb.ToString());

推荐阅读