当我点击一个按钮时,我想要一个消息框,弹出一个薪水最低的员工。这些员工通过文本框输入并存储在数组中。该课程被称为“员工”。
我已经开始工作,但是例如,如果我输入ID:1234,名字:Bob,姓氏:样式,薪水:24,消息框显示:“1234BobStyles24”,没有空格。我怎样才能解决这个问题?这是我的代码:
private void lowestSalaryButton_Click(object sender, EventArgs e)
{
decimal minSalary = employees.Min(em => em.YearSalary);
IEnumerable<Employee> poors = employees.Where(em => em.YearSalary == minSalary);
poors.Count();
foreach (var poor in poors)
MessageBox.Show(minSalary.ToString("C") + poor.FirstName + poor.LastName +
poor.EmployeeId, "Min salary");
}
}
}
答案 0 :(得分:6)
只需将其更改为:
MessageBox.Show(minSalary.ToString("C") + " " + poor.FirstName + " " + poor.LastName + " " + poor.EmployeeId, "Min salary");
或者更好的方法是:
string message = string.Format("{0} {1} {2} {3}", minSalary, poor.FirstName, poor.LastName, poor.EmployeeId);
MessageBox.Show(message, "Min salary");
答案 1 :(得分:2)
这样的事情:
string spaceInBetween =new string(' ', 2);
MessageBox.Show(minSalary.ToString("C") +
spaceInBetween + poor.FirstName +
spaceInBetween + poor.LastName +
spaceInBetween + poor.EmployeeId, "Min salary");
答案 2 :(得分:2)
在您的代码中:
MessageBox.Show(minSalary.ToString("C") + poor.FirstName + poor.LastName +
poor.EmployeeId, "Min salary");
你正是这样做的
您可以使用
String.Format(" {0} {1} [2} {3}, Min salary",minSalary.ToString("C"), poor.FirstName , poor.LastName , poor.EmployeeId )
或者,您可以使用
添加自己的空格poor.Firstname + " " + poor.Lastname