我剛剛正在學習多維數組和消息框。我目前在我的消息框中創建2列時遇到問題。我現在可以打印出需要的隨機數字,但只能在一列中打印。謝謝您的幫助!在MessageBox中顯示多維數組的值
string msg = "";
Random numb = new Random();
int[,] thing = new int[ 10, 2 ];
thing[0, 0] = numb.Next(0,10);
thing[0, 1] = numb.Next(0,10);
thing[1, 0] = numb.Next(0,10);
thing[1, 1] = numb.Next(0,10);
thing[2, 0] = numb.Next(0,10);
thing[2, 1] = numb.Next(0,10);
thing[3, 0] = numb.Next(0,10);
thing[3, 1] = numb.Next(0,10);
thing[4, 0] = numb.Next(0,10);
thing[4, 1] = numb.Next(0,10);
thing[5, 0] = numb.Next(0,10);
thing[5, 1] = numb.Next(0,10);
thing[6, 0] = numb.Next(0,10);
thing[6, 1] = numb.Next(0,10);
thing[7, 0] = numb.Next(0,10);
thing[7, 1] = numb.Next(0,10);
thing[8, 0] = numb.Next(0,10);
thing[8, 1] = numb.Next(0,10);
thing[9, 0] = numb.Next(0,10);
thing[9, 1] = numb.Next(0,10);
foreach (int x in thing)
msg = msg + x + "\n";
MessageBox.Show(msg, "Table");
'MessageBox.Show(味精,「表」);'是'foreach'環外,因此印刷'msg'總是來自最後一次迭代的那個。在C#中,你必須對大多數多行語句使用大括號,比如'foreach',即:要查看所有的值,你必須編寫'foreach(int x in thing) { msg = msg + x +「\ n」; MessageBox.Show(msg,「Table」); }'。 – varocarbas