C#百分比计算未正确计算

时间:2016-02-27 08:49:13

标签: c# percentage tableadapter

我正在开发一个记录数据的简单应用程序,然后允许用户根据该数据检索百分比。通过/失败等

我的整体计算工作得很好,但是当我的3,6,9箱计算发生时,他们似乎没有改变,即使我已经切换了哪个用户应该提取统计数据。

我更改了代码以返回在calc_percent()方法中计算的百分比的格式化字符串。我还用不同的方法封装了我的计算,以便我更容易阅读。

以下是我github project的链接。该项目不包含数据集或tableadapters,我认为我的计算错误可能与我的数据集查询有关,但我不确定。我的印象是表适配器上的Fill()方法会使用该查询的结果填充输入数据集。这应该允许我预先形成进一步的查询以缩小我的结果是否正确?我可能不正确地使用tableadapter查询,但我不确定。

#region Metrics Methods

private void generate_stats(User user)
{

    int crate_total = 0;

    try
    {
        crate_total = (int)this.CratesTableAdapter.count_crates_by_tech(user.Last_name);
        generate_overall_stats(user);

        // last 30 units stats
        if (crate_total >= 3)
        {
            generate_3_crate_stats(user);
        }

        // last 60 units stats
        if (crate_total >= 6)
        {
            generate_6_crate_stats(user);
        }

        // last 90 units stats
        if (crate_total >= 9)
        {
            generate_9_crate_stats(user);
        }

    }
    catch (NullReferenceException e)
    {
        MessageBox.Show(e.Message);
    }
    catch (OleDbException e)
    {
        MessageBox.Show(e.Message);
    }

}
private string calc_percent(int total, int number)
{
    double percentage = 0;

    percentage = ((double)number / total) * 100;

    string format_percent = string.Format("{0:0.00%}", percentage);
    try
    {
        if (percentage == 0)
        {
            throw new NullReferenceException("Calculation Has Failed, Possible Data Inaccuracy");
        }
    }catch(NullReferenceException e)
    {
        MessageBox.Show(e.Message);
    }
    return format_percent;
}
private void generate_overall_stats(User user)
{
    // Overall Stats
    int total = (int)this.Audit_ResultsTableAdapter.count_units_by_user(user.Last_name);
    int pass = (int)this.Audit_ResultsTableAdapter.count_pass_units_by_user(user.Last_name);
    int fail = total - pass;
    string ovr_pass_perc = calc_percent(total, pass);
    string ovr_fail_perc = calc_percent(total, fail);

    metrics_qc_overall_units_display.Text = total.ToString();
    metrics_qc_overall_pass_display.Text = ovr_pass_perc;
    metrics_qc_overall_fail_display.Text = ovr_fail_perc;
}
private void generate_3_crate_stats(User user)
{
    int crate_pass = 0;
    int crate_fail = 0;
    metrics_qc_last30_group.Visible = true;

    // Reset data set
    this.CratesTableAdapter.Fill(this.android_teamDataSet.Crates);
    // Get all crates by user in Desc order according to date (most recent dates at the top of the table)
    this.CratesTableAdapter.get_crates_by_tech(this.android_teamDataSet.Crates, user.Last_name);
    // Get the 3 most recent crates
    this.CratesTableAdapter.get_top_3_crates(this.android_teamDataSet.Crates);

    foreach (DataRow row in this.android_teamDataSet.Crates)
    {
        crate_pass = crate_pass + (int)row["passed_units"];
        crate_fail = crate_fail + (int)row["failed_units"];
    }

    int tmp_total = crate_pass + crate_fail;
    string pass_percent_30 = calc_percent(tmp_total, crate_pass);
    string fail_percent_30 = calc_percent(tmp_total, crate_fail);

    metrics_qc_last30_pass_display.Text = pass_percent_30;
    metrics_qc_last30_fail_display.Text = fail_percent_30;
}
private void generate_6_crate_stats(User user)
{
    int crate_pass = 0;
    int crate_fail = 0;

    metrics_qc_last60_group.Visible = true;

    this.CratesTableAdapter.Fill(this.android_teamDataSet.Crates);
    this.CratesTableAdapter.get_crates_by_tech(this.android_teamDataSet.Crates, user.Last_name);
    this.CratesTableAdapter.get_top_6_crates(this.android_teamDataSet.Crates);

    foreach (DataRow row in this.android_teamDataSet.Crates)
    {
        crate_pass = crate_pass + (int)row["passed_units"];
        crate_fail = crate_fail + (int)row["failed_units"];
    }

    int tmp_total = crate_pass + crate_fail;
    string pass_percent_60 = calc_percent(tmp_total, crate_pass);
    string fail_percent_60 = calc_percent(tmp_total, crate_fail);
    metrics_qc_last60_pass_display.Text = pass_percent_60;
    metrics_qc_last60_fail_display.Text = fail_percent_60;
}
private void generate_9_crate_stats(User user)
{
    int crate_pass = 0;
    int crate_fail = 0;

    metrics_qc_last90_group.Visible = true;
    this.CratesTableAdapter.Fill(this.android_teamDataSet.Crates);
    this.CratesTableAdapter.get_crates_by_tech(this.android_teamDataSet.Crates, user.Last_name);
    this.CratesTableAdapter.get_top_9_crates(this.android_teamDataSet.Crates);

    foreach (DataRow row in this.android_teamDataSet.Crates)
    {
        crate_pass = crate_pass + (int)row["passed_units"];
        crate_fail = crate_fail + (int)row["failed_units"];
    }

    int tmp_total = crate_pass + crate_fail;
    string pass_percent_90 = calc_percent(tmp_total, crate_pass);
    string fail_percent_90 = calc_percent(tmp_total, crate_fail);
    metrics_qc_last90_pass_display.Text = pass_percent_90;
    metrics_qc_last90_fail_display.Text = fail_percent_90;

}
private void set_users_metrics_defaults()
{

    metrics_qc_last30_group.Visible = false;
    metrics_qc_last60_group.Visible = false;
    metrics_qc_last90_group.Visible = false;


}

#endregion

#region Metrics Event Handlers

private void metrics_qc_display_panel_Paint(object sender, PaintEventArgs e)
{

}
private void metrics_auditor_combobox_SelectionChangeCommitted(object sender, EventArgs e)
{

}
private void metrics_qc_combobox_SelectionChangeCommitted(object sender, EventArgs e)
{
    set_users_metrics_defaults();
    User metrics_user = gen_user_for_metrics();
    generate_stats(metrics_user);
}

#endregion

2 个答案:

答案 0 :(得分:1)

现在任何crate_total> 9也会评估> 3和> 6。

if (crate_total >= 3)
if (crate_total >= 6)
if (crate_total >= 9)

您需要在if语句中设置上限和下限。

// last 30 units stats
if (crate_total >= 3 && crate_total < 6)

// last 60 units stats
if (crate_total >= 6 && crate_total < 9)

// last 90 units stats
if (crate_total >= 9)

答案 1 :(得分:0)

我改变了我的计算方式。问题是,当我改变用户时,整体统计数据会发生变化,但30/60/90单位统计数据不会改变,它将始终保持不变。我意识到这些价值并没有因某种原因而被某些方式清除(在驾驶时突然出现在我脑海中)。所以我把所有东西都隔离了。

#region Metrics Methods

        private void generate_stats(User user)
        {

            int crate_total = 0;

            try
            {
                crate_total = (int)this.CratesTableAdapter.count_crates_by_tech(user.Last_name);


                generate_overall_stats(user);

                if (crate_total >= 3)
                {
                    generate_3_crate_stats(user, crate_total);
                }
                if (crate_total >= 6)
                {
                    generate_6_crate_stats(user, crate_total);
                }
                //if (crate_total >= 9)
                //{
                //    generate_9_crate_stats(user, crate_total);
                //}


            }
            catch (NullReferenceException e)
            {
                MessageBox.Show(e.Message);
            }
            catch (OleDbException e)
            {
                MessageBox.Show(e.Message);
            }

        }
        private string calc_percent(int total, int number)
        {
            double percentage = 0;

            percentage = ((double)number / total) * 100;

            string format_percent = string.Format("{0:0.00}%", percentage);
            try
            {
                if (percentage == 0)
                {
                    throw new NullReferenceException("Calculation Has Failed, Possible Data Inaccuracy");
                }
            }catch(NullReferenceException e)
            {
                MessageBox.Show(e.Message);
            }
            return format_percent;
        }
        private void generate_overall_stats(User user)
        {
            // Overall Stats
            int total = (int)this.Audit_ResultsTableAdapter.count_units_by_user(user.Last_name);
            int pass = (int)this.Audit_ResultsTableAdapter.count_pass_units_by_user(user.Last_name);
            int fail = total - pass;
            string ovr_pass_perc = calc_percent(total, pass);
            string ovr_fail_perc = calc_percent(total, fail);

            metrics_qc_overall_units_display.Text = total.ToString();
            metrics_qc_overall_pass_display.Text = ovr_pass_perc;
            metrics_qc_overall_fail_display.Text = ovr_fail_perc;
        }
        private void generate_3_crate_stats(User user, int crate_number)
        {

            metrics_qc_last30_group.Visible = true;
            int[] crate_stats = crate_stats_count_3(user.Last_name, crate_number);

            int tmp_total = crate_stats[0] + crate_stats[1];
            string pass_percent_30 = calc_percent(tmp_total, crate_stats[0]);
            string fail_percent_30 = calc_percent(tmp_total, crate_stats[1]);

            metrics_qc_last30_pass_display.Text = pass_percent_30;
            metrics_qc_last30_fail_display.Text = fail_percent_30;
        }
        private void generate_6_crate_stats(User user, int crate_number)
        {

            metrics_qc_last60_group.Visible = true;

            int[] crate_stats = crate_stats_count_6(user.Last_name, crate_number);

            int tmp_total = crate_stats[0] + crate_stats[1];
            string pass_percent_60 = calc_percent(tmp_total, crate_stats[0]);
            string fail_percent_60 = calc_percent(tmp_total, crate_stats[1]);

            metrics_qc_last60_pass_display.Text = pass_percent_60;
            metrics_qc_last60_fail_display.Text = fail_percent_60;

        }
        private void generate_9_crate_stats(User user, int crate_number)
        {
            metrics_qc_last90_group.Visible = true;

            int[] crate_stats = crate_stats_count_9(user.Last_name, crate_number);

            int tmp_total = crate_stats[0] + crate_stats[1];
            string pass_percent_90 = calc_percent(tmp_total, crate_stats[0]);
            string fail_percent_90 = calc_percent(tmp_total, crate_stats[1]);

            metrics_qc_last90_pass_display.Text = pass_percent_90;
            metrics_qc_last90_fail_display.Text = fail_percent_90;

        }
        private void set_users_metrics_defaults()
        {

            metrics_qc_last30_group.Visible = false;
            metrics_qc_last60_group.Visible = false;
            metrics_qc_last90_group.Visible = false;


        }
        private int[] crate_stats_count_3(string last_name, int crate_number)
        {
            int[] crate_numbers = new int[3];
            crate_numbers[0] = crate_number;
            crate_numbers[1] = crate_number--;
            crate_numbers[2] = crate_number -= 2;
            int crate_pass = 0;
            int crate_fail = 0;
            for (int i = 0; i < 3; i++)
            {
                crate_pass = crate_pass + (int)this.Audit_ResultsTableAdapter.count_pass_units_by_crate_number_and_tech(crate_numbers[i], last_name);
                crate_fail = crate_fail + (int)this.Audit_ResultsTableAdapter.count_fail_units_by_crate_number_and_tech(crate_numbers[i], last_name);
            }
            int[] crate_stats = new int[2];
            crate_stats[0] = crate_pass;
            crate_stats[1] = crate_fail;

            return crate_stats;
        }
        private int[] crate_stats_count_6(string last_name, int crate_number)
        {
            int[] crate_numbers = new int[6];
            crate_numbers[0] = crate_number;
            crate_numbers[1] = crate_number--;
            crate_numbers[2] = crate_number -= 2;
            crate_numbers[3] = crate_number -= 3;
            crate_numbers[4] = crate_number -= 4;
            crate_numbers[5] = crate_number -= 5;

            int crate_pass = 0;
            int crate_fail = 0;
            for (int i = 0; i < 6; i++)
            {
                crate_pass = crate_pass + (int)this.Audit_ResultsTableAdapter.count_pass_units_by_crate_number_and_tech(crate_numbers[i], last_name);
                crate_fail = crate_fail + (int)this.Audit_ResultsTableAdapter.count_fail_units_by_crate_number_and_tech(crate_numbers[i], last_name);
            }
            int[] crate_stats = new int[2];
            crate_stats[0] = crate_pass;
            crate_stats[1] = crate_fail;

            return crate_stats;
        }
        private int[] crate_stats_count_9(string last_name, int crate_number)
        {
            int[] crate_numbers = new int[9];
            crate_numbers[0] = crate_number;
            crate_numbers[1] = crate_number--;
            crate_numbers[2] = crate_number -= 2;
            crate_numbers[3] = crate_number -= 3;
            crate_numbers[4] = crate_number -= 4;
            crate_numbers[5] = crate_number -= 5;
            crate_numbers[6] = crate_number -= 6;
            crate_numbers[7] = crate_number -= 7;
            crate_numbers[8] = crate_number -= 8;

            int crate_pass = 0;
            int crate_fail = 0;

            for (int i = 0; i < 9; i++)
            {
                crate_pass = crate_pass + (int)this.Audit_ResultsTableAdapter.count_pass_units_by_crate_number_and_tech(crate_numbers[i], last_name);
                crate_fail = crate_fail + (int)this.Audit_ResultsTableAdapter.count_fail_units_by_crate_number_and_tech(crate_numbers[i], last_name);
            }
            int[] crate_stats = new int[2];
            crate_stats[0] = crate_pass;
            crate_stats[1] = crate_fail;

            return crate_stats;
        }

        #endregion

上面显示的代码根据crate_number / repair_technician的count()查询计算出有希望的正确百分比。现在,当组合框中的名称更改时,stats正确更新。

出于某种原因出现一个问题,我的60和90个统计数据保持不变。我还没有看过去发现这个错误,但我会在今晚或明天解决。