硬币计数器为每个确切的硬币添加双倍值

时间:2017-01-31 22:08:25

标签: c#

我正在做一个C#程序,假设从0-99(给定的更改量)取一个数量,程序应该将更改放在正确的位置。一个例子是我输入:

给出的变更金额:68

它应该读作:

第2季

尺寸1

Nickles 1

Pennies 3

我在2015年的视觉工作室中使用Windows表单应用程序。

每当我输入1的数量时,它应该是1便士但它给它2,相同的角钱镍和季度。其他任何工作都很好。

这就是我的代码:

private void calcButton_Click(object sender, EventArgs e){
    //declare variables

    const int QUARTERS = 25;
    const int DIMES = 10;
    const int NICKELS = 5;
    const int PENNIES = 1;
    int changeVal = 0;

    //validations
    if (int.TryParse(changeTextBox.Text, out changeVal)){
        if (changeVal > 0 && changeVal < 100){
            //calcs each value with the remainder operator divided by each change amount
            int quarterVal = changeVal / QUARTERS;
            int dimeVal = ((changeVal % QUARTERS) / DIMES);
            int nickelVal = (((changeVal % QUARTERS) % DIMES) / NICKELS);
            int pennyVal = ((((changeVal % QUARTERS) % DIMES) % NICKELS) / PENNIES);

            //if statements to put each coin in each specific value variable
            if (changeVal == QUARTERS){
                quarterVal++;
                changeVal = changeVal - QUARTERS;
            }
            else if (changeVal == DIMES){
                dimeVal++;
                changeVal = changeVal - DIMES;
            }
            else if (changeVal == NICKELS){
                nickelVal++;
                changeVal = changeVal - NICKELS;
            }
            else if (changeVal == PENNIES){
                pennyVal++;
                changeVal -= changeVal - PENNIES;
            }

            //display

            quartersLabel.Text = quarterVal.ToString();
            dimesLabel.Text = dimeVal.ToString();
            nickelsLabel.Text = nickelVal.ToString();
            penniesLabel.Text = pennyVal.ToString();
        }

        //error message
        else{
            MessageBox.Show("You have to enter a numeric value greater than 0 and less than 100");
        }


    }
    //error message
    else{
        MessageBox.Show("Please enter a numeric value");
    }
}

如果你能告诉我,我做错了什么会非常有用!

2 个答案:

答案 0 :(得分:0)

您正在将ChangeVal与硬币的面值进行比较。然后在它们相等时递增它,

此部分仅在您的changeVal等于硬币的面值时运行 因此,1,5,10,25都产生双重计数。只需删除以下部分即可。

      if (changeVal == QUARTERS)
                {
                    quarterVal++;
                    changeVal = changeVal - QUARTERS;
                }
                else if (changeVal == DIMES)
                {
                    dimeVal++;
                    changeVal = changeVal - DIMES;
                }
                else if (changeVal == NICKELS)
                {
                    nickelVal++;
                    changeVal = changeVal - NICKELS;
                }
                else if (changeVal == PENNIES)
                {
                    pennyVal++;
                    changeVal -= changeVal - PENNIES;
                }

答案 1 :(得分:0)

您已经使用以下代码计算所需的硬币数量:

int quarterVal = changeVal / QUARTERS;
etc....

但是如果changeVal等于你的一个预定义常量,那么你将它递增1。

基本上,当输入的值与您定义的常量之一完全匹配时,您不需要增加计数的4 if语句。