Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#使用messagebox.show将计算出的迭代存储并显示在一行中_C# - Fatal编程技术网

C#使用messagebox.show将计算出的迭代存储并显示在一行中

C#使用messagebox.show将计算出的迭代存储并显示在一行中,c#,C#,我正在做这个项目。我的目的是将计算输入数据的结果存储在一个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

我正在做这个项目。我的目的是将计算输入数据的结果存储在一个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

我真的很感谢你的帮助


亲切问候

您只需在循环中加入字符串,然后在消息框中的循环外显示它们。使用
StringBuilder
类附加结果

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

MessageBox.Show(sb.ToString());

WriteLine
更改为
Write
,然后在
循环后调用
控制台.WriteLine()
。否则,在循环期间,您需要在
字符串结果
(或者更好的是,
StringBuilder
)中捕获结果,然后在循环完成后输出
结果
。您几乎无法控制MessageBox文本的格式。显示友好消息告诉用户出了什么问题是一种方便的方法,显示程序结果是非常不合适的。在Winforms应用程序中使用标签,但仅在与程序的预期用户交谈后使用。他们会要求其他东西。谢谢您的回复,我可以知道如何在我的情况下转换StringBuilder并更改为
int
变量吗。