2

当我单击一个按钮时,我希望弹出一个消息框,显示工资最低的员工。这些员工通过文本框输入并存储在数组中。该类称为“员工”。

我已经让它工作了,但是例如,如果我输入 ID:1234,名字:Bob,姓氏:Styles,Salary: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");

                }
            }
        }
4

3 回答 3

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");
于 2012-04-17T16:17:47.193 回答
2

像这样的东西:

      string spaceInBetween  =new string(' ', 2);
      MessageBox.Show(minSalary.ToString("C") + 
                  spaceInBetween   +  poor.FirstName + 
                  spaceInBetween   + poor.LastName +  
                  spaceInBetween + poor.EmployeeId, "Min salary");
于 2012-04-17T16:18:43.010 回答
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
于 2012-04-17T16:19:21.030 回答