简体   繁体   English

使用 C# 更改计算器

[英]Change calculator using C#

I am trying to write a program that takes price and amount paid and outputs the change denominations.我正在尝试编写一个程序来获取支付的价格和金额并输出零钱面额。 I am having issues with the following.我遇到以下问题。

  • I will also like only applicable change denominations to be printed (Ie, if change is 71, only show FithyBills =1, TwentyBills=1 and Bills =1 and ignore the rest).我还希望只打印适用的零钱面额(即,如果零钱为 71,则只显示 FithyBills =1、TwentyBills=1 和 Bills =1 并忽略其余部分)。 Currently, it shows all with other recorded as Ten Bills=0 and so on).目前显示全部,其他记录为十张=0,以此类推)。

Here is the code这是代码

using System;

namespace ChangeCalulator
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Item Price: ");
            int price = Convert.ToInt32(Console.ReadLine());
            Console.Write("Amount Paid: ");
            int paid = Convert.ToInt32(Console.ReadLine());
            while (paid < pice)
            {
                Console.WriteLine("You have paid less than the price");
                Console.Write("Paid: ");
                paid = Convert.ToInt32(Console.ReadLine());

            }
            decimal changeamount = paid - price;

            Change change = new Change(changeamount);
            //This displays the change based on each denominantion
            Console.WriteLine($"Hundred SEK Bills: {change.HundredBills}");
            Console.WriteLine($"Fifty SEK Bills: {change.FiveBills}");
            Console.WriteLine($"Twenty SEK Bills: {change.TwentyBills}");
            Console.WriteLine($"Ten SEK Bills: {change.TenBills}");
            Console.WriteLine($"Five SEK Bills: {change.FiveBills}");
            Console.WriteLine($"One SEK: {change.Bills}");

            Console.ReadKey();
        }
    }
    //The Change class  will create a public integer variable for each denomination and store the values.
    public class Change
    {
        private int changeamount;

        public int HundredBills { get; }
        public int FiftyBills { get; }
        public int TwentyBills { get; }
        public int TenBills { get; }
        public int FiveBills { get; }
        public int Bills { get; }
        //This calculates the number of each denomination the amount equals
        public Change(decimal price)
        {
            HundredBills = (int)(changeamount / 100);
            price %= 100;

            FiftyBills = (int)(changeamount / 50);
            price %= 50;

            TwentyBills = (int)(changeamount / 20);
            price %= 20;

            TenBills = (int)(changeamount / 10);
            price %= 10;

            FiveBills = (int)(changeamount / 5);
            price %= 5;

            Bills = (int)(changeamount / 1);
            price %= 1;
        }
    }
}

Any help on this is greatly appreciated.非常感谢您对此的任何帮助。

In the constructor public Change(decimal price) , you need to use price :在构造函数public Change(decimal price)中,您需要使用price

public Change(decimal price)
{
    HundredBills = (int)(price / 100);
    price %= 100;

    FiftyBills = (int)(price / 50);
    price %= 50;

    TwentyBills = (int)(price / 20);
    price %= 20;

    TenBills = (int)(price / 10);
    price %= 10;

    FiveBills = (int)(price / 5);
    price %= 5;

    Bills = (int)(price / 1);
    price %= 1;
}

You can then remove the unneeded changeamount field.然后,您可以删除不需要的changeamount字段。

Working demo: https://dotnetfiddle.net/NbyZPc工作演示: https://dotnetfiddle.net/NbyZPc

As for not printing the 0 amounts, a simple if (theValue > 0) check around each WriteLine should accomplish that.至于不打印 0 金额,一个简单的if (theValue > 0)检查每个 WriteLine 应该可以完成。

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

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