【问题标题】:Take the sum of a list as variable and edit it at runtime将列表的总和作为变量并在运行时对其进行编辑
【发布时间】:2017-07-07 13:51:29
【问题描述】:

我正在尝试计算取决于是否选中复选框的特定变量的最终值。然而,这是动态完成的。

我想要达到的目标:

上面的例子工作正常,但是当我开始取消选中复选框时它开始出错。它减去了两倍的时间,我不知道为什么:

我的代码(TimeSpent 是我最后想要的变量):

public class Activity
{
    public int ID { get; set; }
    public int IncidentID { get; set; }
    public string Description { get; set; }
    public int TimeSpent { get; set; }
    public static int StartX { get; set; } = 10;
    public static int StartY { get; set; } = 10;

    private TextBox textBox = null;

    private CheckBox checkBox = null;

    public Activity(int iD, int incidentID, string description, int timeSpent)
    {
        this.ID = iD;
        this.IncidentID = incidentID;
        this.Description = description;
        this.TimeSpent = timeSpent;
    }

    public Activity()
    { }

    public bool isChecked() {
        return checkBox.Checked;
    }

    public void setTimeSpent(int timeSpent) {
        this.TimeSpent = timeSpent;
        textBox.Text = timeSpent.ToString();
    }

    public int getTimeSpent()
    {
        return Int32.Parse(textBox.Text);
    }

    public void DrawToForm(Panel p)
    {    
        var label = new Label();
        textBox = new TextBox();
        checkBox = new CheckBox();
        checkBox.Checked = true;
        textBox.Size = new System.Drawing.Size(40, 20);
        label.Text = Description.ToString();
        label.AutoSize = true;
        textBox.Text = TimeSpent.ToString();
        label.Left = StartX;
        label.Top = StartY;
        StartX += 430;// Move position to right
        textBox.Left = StartX;
        textBox.Top = StartY;
        StartX += 160;// Move position to right
        checkBox.Left = StartX;
        checkBox.Top = StartY;
        StartX = 10;// Reset to start
        StartY += 50;// Move position to down
        p.Controls.Add(label);
        p.Controls.Add(textBox);
        p.Controls.Add(checkBox);
    }
}

图形界面:

private void Btn_Validate_Click(object sender, EventArgs e)
{      
    tb_TotalTime.Text = calculateTotalTime().ToString();
}

public int calculateTotalTime()
{
    int total = 0;
    foreach (Activity a in activities)
    {                  
        if(a.isChecked())
        {
            total += a.getTimeSpent();
        }else
        {
            total -= a.getTimeSpent();
        }
    }
    return total;
}

为什么减法没有正确完成?

【问题讨论】:

  • 在图片中看起来一切正常。具体问题是什么?
  • 检查时加,非检查时减去 -5-5+5 等于 -5。我想你只是想要添加所以删除else
  • 计算正确。对于两个未选中的复选框,它变为 -10,对于一个选中的复选框,它的 5 和它们的总和变为 -5。根据您的期望,结果应该是什么?如果您希望结果为 5,则不应进行减法。

标签: c# subtraction


【解决方案1】:

复选框未选中时你减去:

if(a.isChecked())
{
    total += a.getTimeSpent();
}
else
{
    total -= a.getTimeSpent();// << THIS
}

因此,设置您的第二张图片(顶部 2 个未选中底部一个已选中)您可以这样做:

0 - 5 - 5 + 5 = -5

0来自默认值Total (int)


要解决此问题,您只需删除 else 子句,因为您不需要减去计费时间永远

【讨论】:

  • 我没想到,这就是问题所在。那根本不是编码问题。更多的是常识问题
  • @TimSchmelter 因为一开始总计为 0。然后减去所有未检查的活动时间。第一个是5,第二个是5,第三个是5。第一个和第二个不检查。总和为 0 - 5 - 5 + 5 = -5。
  • @TimSchmelter 你是怎么得出这个结论的?毫无意义
  • 他现在好像明白了
  • @KahnKah 没问题,这些问题的最佳解决方案是通过代码,就像你在解释它一样。当你到达- 部分时,你会说“那确实……嗯……那不应该存在”(谷歌橡皮鸭调试)。祝你好运!
【解决方案2】:

您应该完全删除减法。 所以改成:

if(a.isChecked())
{
     total += a.getTimeSpent();
}

您有total=0,在计算之前您还没有添加所有值。所以你不需要减去。

【讨论】:

  • 非常感谢,我没有注意到这一点
【解决方案3】:

你的逻辑是加如果检查减如果未检查。如果仅选中,则应添加您的逻辑。

private void Btn_Validate_Click(object sender, EventArgs e)
    {

        tb_TotalTime.Text = calculateTotalTime().ToString();
    }

    public int calculateTotalTime()
    {
        int total = 0;
        foreach (Activity a in activities)
        {

            if (a.isChecked())
            {
                total += a.getTimeSpent();
            }
        }
        return total;
    }

【讨论】:

  • 非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-04
相关资源
最近更新 更多