C#Can';无法获取MessageBox以显示变量
它很好地显示了我的结果,但我想添加以下对话框“一个长为l、宽为w、高为h的框,其体积为x”(l、w、h和x是用户的输入)C#Can';无法获取MessageBox以显示变量,c#,variables,messagebox,C#,Variables,Messagebox,它很好地显示了我的结果,但我想添加以下对话框“一个长为l、宽为w、高为h的框,其体积为x”(l、w、h和x是用户的输入) double heightLabelBox; double lengthLabelBox; double widthLabelBox; double result; heightLabelBox = double.Parse(heightTextBox.Text); lengthLabelBox = double.Parse(len
double heightLabelBox;
double lengthLabelBox;
double widthLabelBox;
double result;
heightLabelBox = double.Parse(heightTextBox.Text);
lengthLabelBox = double.Parse(lengthTextBox.Text);
widthLabelBox = double.Parse(lengthTextBox.Text);
result = heightLabelBox * lengthLabelBox * widthLabelBox;
MessageBox.Show(result.ToString());
试试这个:
MessageBox.Show(string.format("A box with length {0}, width {1} and height {2} has a volume of {3}", lengthLabelBox, widthLabelBox, heightLabelBox, result));
但是,尝试以这种方式对其进行格式化没有意义,因为您已经有了文本框值,因此只需使用string.Join或“+”运算符将其连接在一起,或者使用string.format about,只需将文本框值替换为文本框值,并将其替换为解析值
您还可以使用StringBuilder:
var sb = new StringBuilder(50); // give it an initial value/capacity.
sb.AppendFormat("A box with length {0}, width {1} and height {2} has a volume of {3}", lengthTextBox.Text, widthTextBox.Text, heightTextBox.Text, result);
MessageBox.Show(sb.ToString());
messagebox.show的输出是什么?请先尝试将双变量转换为字符串/或,然后使用string.Format创建新变量,或在messagebox声明本身中创建新变量。您可能还希望将双值缩短到小数位数以下。为什么不将字符串与结果合并在一起。ToString()?您尝试过什么?c语言中的google字符串连接?请不要忘记添加检查,以查看文本框中的值是否实际上是有效的双精度值。