C#,值得覆盖而不是保留旧值并添加新值

时间:2014-08-01 11:34:15

标签: c# asp.net

我正在努力使其总和的值是所有employeePer*的总和,例如,如果有两名员工employeePer1= 2employeePer2 = 2,则总数应该是4,输入前两个if语句。但是,总是为totalPer分配它进入的最后if语句的值,例如在上面的示例totalper中= = 2而不是4, 谢谢你的帮助

protected void Page_Load(object sender, EventArgs e)
{
    int numberOfEmps = Convert.ToInt32(Request.QueryString["employees"]);
    int totalPercent = 100*(Convert.ToInt32(Request.QueryString["employees"]));
    Response.Write(totalPercent);
    int totalPer = 0;

    if(numberOfEmps == 1)
    {
    int employee1Per =  (Convert.ToInt32(Request.QueryString["employee1Per"]));
    Response.Write(employee1Per);
    int employee1Att = (Convert.ToInt32(Request.QueryString["employee1Att"]));
    int employee1Sal = (Convert.ToInt32(Request.QueryString["employee1Sal"]));
    int employee1Yer = (Convert.ToInt32(Request.QueryString["employee1Yer"]));
        totalPer += employee1Per;
    }
    if(numberOfEmps == 2)
    {
    int employee2Per = (Convert.ToInt32(Request.QueryString["employee2Per"]));
    int employee2Att = (Convert.ToInt32(Request.QueryString["employee2Att"]));
    int employee2Sal = (Convert.ToInt32(Request.QueryString["employee2Sal"]));
    int employee2Yer = (Convert.ToInt32(Request.QueryString["employee2Yer"]));
        totalPer +=  employee2Per;
    }
    if(numberOfEmps == 3)
    {
    int employee3Per = (Convert.ToInt32(Request.QueryString["employee3Per"]));
    int employee3Att = (Convert.ToInt32(Request.QueryString["employee3Att"]));
    int employee3Sal = (Convert.ToInt32(Request.QueryString["employee3Sal"]));
    int employee3Yer = (Convert.ToInt32(Request.QueryString["employee3Yer"]));
        totalPer +=employee3Per;
    }
    if(numberOfEmps == 4)
    {
    int employee4Per = (Convert.ToInt32(Request.QueryString["employee4Per"]));
    int employee4Att = (Convert.ToInt32(Request.QueryString["employee4Att"]));
    int employee4Sal = (Convert.ToInt32(Request.QueryString["employee4Sal"]));
    int employee4Yer = (Convert.ToInt32(Request.QueryString["employee4Yer"]));
        totalPer +=employee4Per;
    }
    if(numberOfEmps == 5)
    {
    int employee5Per = (Convert.ToInt32(Request.QueryString["employee5Per"]));
    int employee5Att = (Convert.ToInt32(Request.QueryString["employee5Att"]));
    int employee5Sal = (Convert.ToInt32(Request.QueryString["employee5Sal"]));
    int employee5Yer = (Convert.ToInt32(Request.QueryString["employee5Yer"]));
        totalPer +=employee5Per;
    }
    if(numberOfEmps == 6)
    {
    int employee6Per = (Convert.ToInt32(Request.QueryString["employee6Per"]));
    int employee6Att = (Convert.ToInt32(Request.QueryString["employee6Att"]));
    int employee6Sal = (Convert.ToInt32(Request.QueryString["employee6Sal"]));
    int employee6Yer = (Convert.ToInt32(Request.QueryString["employee6Yer"]));
        totalPer +=employee6Per;
    }
    if(numberOfEmps == 7)
    {
    int employee7Per = (Convert.ToInt32(Request.QueryString["employee7Per"]));
    int employee7Att = (Convert.ToInt32(Request.QueryString["employee7Att"]));
    int employee7Sal = (Convert.ToInt32(Request.QueryString["employee7Sal"]));
    int employee7Yer = (Convert.ToInt32(Request.QueryString["employee7Yer"]));
        totalPer +=employee7Per;
    }
    if(numberOfEmps == 8)
    {
    int employee8Per = (Convert.ToInt32(Request.QueryString["employee8Per"]));
    int employee8Att = (Convert.ToInt32(Request.QueryString["employee8Att"]));
    int employee8Sal = (Convert.ToInt32(Request.QueryString["employee8Sal"]));
    int employee8Yer = (Convert.ToInt32(Request.QueryString["employee8Yer"]));
        totalPer +=employee8Per;
    }
    if(numberOfEmps == 9)
    {
    int employee9Per = (Convert.ToInt32(Request.QueryString["employee9Per"]));
    int employee9Att = (Convert.ToInt32(Request.QueryString["employee9Att"]));
    int employee9Sal = (Convert.ToInt32(Request.QueryString["employee9Sal"]));
    int employee9Yer = (Convert.ToInt32(Request.QueryString["employee9Yer"]));
        totalPer +=employee9Per;
    }
    if(numberOfEmps == 10)
    {
    int employee10Per = (Convert.ToInt32(Request.QueryString["employee10Per"]));
    int employee10Att = (Convert.ToInt32(Request.QueryString["employee10Att"]));
    int employee10Sal = (Convert.ToInt32(Request.QueryString["employee10Sal"]));
    int employee10Yer = (Convert.ToInt32(Request.QueryString["employee10Yer"]));
        totalPer +=employee10Per;
    }
    if(numberOfEmps == 11)
    {
    int employee11Per = (Convert.ToInt32(Request.QueryString["employee11Per"]));
    int employee11Att = (Convert.ToInt32(Request.QueryString["employee11Att"]));
    int employee11Sal = (Convert.ToInt32(Request.QueryString["employee11Sal"]));
    int employee11Yer = (Convert.ToInt32(Request.QueryString["employee11Yer"]));
        totalPer +=employee11Per;
    }
    if(numberOfEmps == 12)
    {
    int employee12Per = (Convert.ToInt32(Request.QueryString["employee12Per"]));
    int employee12Att = (Convert.ToInt32(Request.QueryString["employee12Att"]));
    int employee12Sal = (Convert.ToInt32(Request.QueryString["employee12Sal"]));
    int employee12Yer = (Convert.ToInt32(Request.QueryString["employee12Yer"]));
        totalPer +=employee12Per;
    }

    Label7.Text = (totalPer.ToString());
    Label9.Text = (totalPercent.ToString());

}

3 个答案:

答案 0 :(得分:7)

如果有两名员工,代码不会输入前两个if州名表,只会输入第二名。

对于第一个if语句,您需要检查是否有一名或多名员工,而不是一名员工:

if(numberOfEmps >= 1)

然后对应所有其他if语句。

答案 1 :(得分:1)

你可以在这里做很多很多的重构,虽然这个问题已得到正确回答,但我觉得你可能会从另一个角度受益,以达到你想要的目标。做。

首先,我有一些结构来存储我的员工,所以让我们使用struct例如:

struct Employee
{
    int Per;
    int Att;
    int Sal;
    int Yer;
}

现在,因为员工人数是一个变量,你所拥有的数字不会超过12,所以我for到达给定的数字并创建Employee的实例来自QueryString数据,暂时将它们存储在某种集合List<T>中,如下所示:

List<Employee> employees = new List<Employee>();    
int numberOfEmps = Convert.ToInt32(Request.QueryString["employees"]);
for (int i = 1; i <= numberOfEmps; i++)
{
    Employee employee = new Employee()
    {
        Per =  Convert.ToInt32(Request.QueryString[string.Format("employee{0}Per", i)]),
        Att = Convert.ToInt32(Request.QueryString[string.Format("employee{0}Att", i)]),
        Sal = Convert.ToInt32(Request.QueryString[string.Format("employee{0}Sal", i)]),
        Yer = Convert.ToInt32(Request.QueryString[string.Format("employee{0}Yer", i)])
    };
    employees.Add(employee);
}

然后你有2个选项来计算你的Per属性,你可以使用迭代器:

int totalPer = 0;
foreach (var employee in employees)
{
    totalPer = totalPer + employee.Per;
}

或者您可以使用LINQ

totalPer = employees.Sum(e => e.Per);

答案 2 :(得分:0)

你可以使用迭代使这更简单,因为看起来你做的不多 与员工[x]每人,员工[x] Att和员工[x] Sal

类似的东西:

int numberOfEmps = Convert.ToInt32(Request.QueryString["employees"]);
int totalPercent = 100*(Convert.ToInt32(Request.QueryString["employees"]));
Response.Write(totalPercent);
int totalPer = 0;

for(int i = 1; i <= numberOfEmps; i++)
{
   int employeePer = (Convert.ToInt32(Request.QueryString["employee" + i + "Per"]));
   totalPer +=employeePer;
}