繁体   English   中英

C#变更程序

[英]C# change making program

制作一个用于家庭作业的零钱制作程序,当输入金额时(它基于澳大利亚货币)它必须退还零钱的数量,我已经将其提高到了50分。 计算出更改后,程序必须返回20美分,10美分或5美分的值时,程序冻结

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void btnCalculate_Click(object sender, EventArgs e)
    {
        double change = Convert.ToDouble(txtOffered.Text) - Convert.ToDouble(txtDue.Text);
        // MessageBox.Show(change.ToString());            
        double hund = 100;
        double fifty = 50;
        double twent = 20;
        double ten = 10;
        double five = 5;
        double two = 2;
        double one = 1;
        double fifcent = 0.50;
        double twentcent = 0.20;
        double tencent = 0.10;
        double fivecent = 0.05;


        while (change > 0) 
        {
            if (change >= hund) 
            {
                txtChange.Text += "1x $100 \r\n"; 
                change = change - hund;
            }


            else if (change >= fifty)
            {
                txtChange.Text += "1x $50 \r\n";
                change = change - fifty;
            }
            if (change >= twent) 
            {
                txtChange.Text += "1x $20 \r\n";
                change = change - twent;
            }
            else if (change >= ten)
            {
                txtChange.Text += "1x $10 \r\n";
                change = change - ten;
            }
            if (change >= five)
            {
                txtChange.Text += "1x $5 \r\n";
                change = change - five;
            }
            else if (change >= two)
            {
                txtChange.Text += "1x $2 \r\n";
                change = change - two;
            }
            if (change >= one)
            {
                txtChange.Text += "1x $1 \r\n";
                change = change - one;
            }
            else if (change >= fifcent)
            {
                txtChange.Text += "1x 50c \r\n";
                change = change - fifcent;
            }
            if (change >= twentcent)
            {
                txtChange.Text += "1x 20c \r\n";
                change = change - twentcent;
            }
            else if (change >= tencent)
            {
                txtChange.Text += "1x 10c \r\n";
                change = change - tencent;
            }
            if (change >= fivecent)
            {
                txtChange.Text += "1x 5c \r\n";
                change = change - fivecent;
            }

        }

    }
}

如果您输入金额<0.05或更改结果后收到金额<0.05,则应用程序将卡住。

原因在这里:如果您的更改变量值> 0,但<0.05,则将永远卡住。

while (change > 0)
{
    ...
    if (change >= fivecent)
    {
        txtChange.Text += "1x 5c \r\n";
        change = change - fivecent;
    }
}

对于新手来说,这也许很难发现。 代码的问题是您使用了错误的DataType。 您应该使用decimal而不是double

decimal change = Convert.ToDouble(txtOffered.Text) - Convert.ToDouble(txtDue.Text);
// MessageBox.Show(change.ToString());            
decimal hund = 100;
decimal fifty = 50;
decimal twent = 20;
decimal ten = 10;
decimal five = 5;
decimal two = 2;
decimal one = 1;
decimal fifcent = 0.50m;
decimal twentcent = 0.20m;
decimal tencent = 0.10m;
decimal fivecent = 0.05m;

这样,您的代码就不会冻结。

对于这个解释,问题在于,使用double的结果0.25 - 0.20是... 0.049999999999999989 这是因为double使用浮点值,这会导致舍入问题。 如果您想进一步了解浮点计算,请看这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM