C#减少或增加变量(如点购系统)?

时间:2012-08-16 07:59:10

标签: c# variables points

我的大脑绝对在这里炒。对(毫无疑问)显而易见的问题感到抱歉,但我看不到木头的树木了!

我有一个充当“点池”的变量。 numericupdown控件会影响此点池。如果我增加numupdown,则点数减少,反之亦然。我无法理解逻辑:(

这是我的“代码”,它的价值......

private void numJobSkill1_ValueChanged(object sender, EventArgs e)
    {

        int difference = (int)(numJobSkill1.Value - numJobSkill1.Minimum);

        /* if (numJobSkill1.Value > numJobSkill1.Minimum)
        {

            POINTPOOL = POINTPOOL - 1;

        } 
        else
        {
            POINTPOOL = POINTPOOL + 1;
        } */

        lblPOINTPOOL.Text = PLAYERPOINTS.ToString();

    }

提前致谢。

3 个答案:

答案 0 :(得分:1)

要确定该值是增加还是减少,您需要记住最后一个值。

// initialize this with the initial value of the UpDownControl
private int _previousValue;

private void numJobSkill1_ValueChanged(object sender, EventArgs e) 
{ 
    int currentValue = numJobSkill1.Value;
    _pointPool -= currentValue - _previousValue;
    _previousValue = currentValue;
} 

答案 1 :(得分:0)

我认为最简单的处理方法是使PointPool成为一个函数而不是一个变量,其中返回值是TotalPointsAvailable - TotalPointAllocated,TotalPointsAllocated是numJobSkill1中的值,但它很容易就是几个updowns的总和。

private void numJobSkill1_ValueChanged(object sender, EventArgs e)
{
    PLAYERPOINTS = PointPool();
    lblPOINTPOOL.Text = PLAYERPOINTS.ToString();

}

private Int32 TotalPointsAvailable;

private Int32 TotalPointsAllocated()
{
   //Value is a decimal
   return (Int32)numJobSkill1.Value;
}

private Int32 PointPool()
{
    return TotalPointsAvailable-TotalPointsAllocated();
} 

答案 2 :(得分:-1)

试试这个:

private void numJobSkill1_ValueChanged(object sender, EventArgs e)
    {

        int difference = (int)(numJobSkill1.Value - numJobSkill1.Minimum);

        if (difference > 0)
        {
            difference--;
        }
        else
        {
            difference++;
        }

        lblPOINTPOOL.Text = difference.ToString();

    }
相关问题