while循环使用If条件导致意外的无限循环

时间:2014-04-23 01:14:33

标签: c# if-statement while-loop

我正在重新审视我的一个旧练习,而且我的逻辑似乎有问题。这是一个简单的控制台应用程序,应该计算和显示给定总额和现金投标的变化。出于某种原因,当我输入小数除25之外的值时,我得到一个无限循环。

以下是我的控制台应用程序的屏幕截图 enter image description here

这是我的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace James_Archbold_A2
{
    class Program
    {
        static void Main(string[] args)
        {
            double change,total,cash;
            int  fifties = 0, twenties = 0, tens = 0, fives = 0, ones = 0,
                quarters = 0, dimes = 0, nickels = 0, pennies = 0;

            Console.Write("Please enter the total: ");
            total = Convert.ToDouble(Console.ReadLine());

            Console.Write("Please enter the cash tendered: ");
            cash = Convert.ToDouble((Console.ReadLine()));

            change = CalculateChange(total, cash);
            Console.WriteLine(change);

            change *= 100;


            while (change > 0)
            {


                if (change >= 5000)
                {
                    change -= 5000;
                    fifties++;
                    Console.WriteLine(change);


                }

                else if (change >= 2000)
                {
                    change -=2000;
                    twenties++;
                    Console.WriteLine(change);

                }

                else if (change >= 1000)
                {
                    change -=1000;
                    tens++;
                    Console.WriteLine(change);

                }

                else if (change >= 500)
                {
                    change -= 500;
                    fives++;
                    Console.WriteLine(change);
                }

                else if (change >= 100)
                {
                    change -=100;
                    ones++;
                    Console.WriteLine(change);

                }

                else if (change >= 25)
                {
                    change -=25;
                    quarters++;
                    Console.WriteLine(change);

                }

                else if (change >= 10)
                { 
                    change -=10;
                    dimes++;
                    Console.WriteLine(change);
                }

                else if (change >= 5)
                {
                    change -=5;
                    nickels++;
                    Console.WriteLine(change);

                }

                else if (change >= 1)
                {
                    change -=1;
                    pennies++;
                    Console.WriteLine(change);
                }
            }

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("Total: {0:c}\nCash Tendered: {1:c}" +
                            "\nChange Due: \nFifties: {2}\nTwenties: {3}\nTens: {4}" +
                              "\nFives: {5}\nOnes: {6}\nQuarters: {7}\nDimes: {8}" +
                              "\nNickels: {9}\nPennies: {10}",
                              total, cash, fifties, twenties, tens, fives, ones, quarters, dimes,nickels, pennies);
            Console.WriteLine(sb);
            Console.ReadLine();
        }

        static double CalculateChange(double total, double cash)
        {
            return cash - total;
        }
    }
}

5 个答案:

答案 0 :(得分:4)

如果您正在编写与财务计算有关的任何内容,请使用decimal数据类型。 不是double

我认为Jon Skeet的文章“Binary floating point and .NET”会给你最深刻的见解。

其他一些指示:

Is a double really unsuitable for money?

What Every Computer Scientist Should Know About Floating Point

答案 1 :(得分:1)

使change int截断.0000000000001,阻止change使用当前算法降至0以下:

int change = CalculateChange(total, cash) * 100;

答案 2 :(得分:0)

在条件

中使用更改> = 0

答案 3 :(得分:0)

你正在使用双打算法,you can't rely on exact precision for decimal input using doubles

如果要使用带小数的浮点数,则应使用C#decimal类型。

答案 4 :(得分:0)

在提供的代码片段中,将变量值更改为其最接近的整数,您将获得所需的输出。

`while (change > 0)`

`while(Math.Round(change)>0)`

希望这有帮助