-1

我有一个象牙,我被要求编写一个程序来计算现金交易后给客户的零钱,并确定每种面额的纸币和硬币的数量。

用户必须输入商品成本和从客户那里收到的金额。

我必须有一个类,它的方法接受十进制参数、Cost 和 Chashreceived,以及整数参数:Hunderds、Fifties、Twenties、Tens、Fives、Twos、Ones、50c、10c、5c、2c 和 1c。

我从收到的现金中减去成本,并计算需要作为找零退回的纸币和硬币的确切数量。

我已经尝试过了,但是当我必须放入硬币时它变得有问题。

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

namespace ChangeCalculator
{
class Program
{
    static void Main(string[] args)
    {
        clsCash Money = new clsCash();
        clsCash Paid = new clsCash();
        Console.WriteLine("What is the cost of the goods?");
        Money.Cost = Convert.ToDecimal(Console.ReadLine());
        Console.WriteLine("How much was recived?");
        Paid.CashRecieved = Convert.ToDecimal(Console.ReadLine());
        Money.GetChange(Money.Cost, Paid.CashRecieved);
        Console.Read();
    }
}

class clsCash
{


private decimal cost;
private decimal cashRecieved;
public decimal Cost
{
    get
    {
        return cost;
    }
    set
    {
        cost = value;
    }
}
public decimal CashRecieved
{
    get
    {
        return cashRecieved;
    }
    set
    {
        cashRecieved = value;
    }
}




public void GetChange(decimal Cost, decimal CashRecieved)
{

    decimal change = CashRecieved - Cost;
    int hundreds = 0;
    int fifty = 0;
    int twenty = 0;
    int ten = 0;
    int five = 0;
    int two = 0;
    int one = 0;
    int centsfifty = 0;
    int centsten = 0;
    int centsfive = 0;
    int centstwo = 0;
    int centsone = 0;
    do
   {

    if (change >= 100)
    {
         hundreds = (int)change / 100;
         change = (int)change % 100;
    } //while (change > 0);
    else if (change >= 50)
    {
        fifty = (int)change / 50;
        change = change % 50;
    }
    else if (change >= 20)
    {
        twenty = (int)change / 20;
        change = change % 20;
    }
    else if (change >= 10)
    {
        ten = (int)change / 10;
        change = change % 10;
    }
    else if (change >= 5)
    {
        five = (int)change / 5;
        change = change % 5;
    }
    else if (change >= 2)
    {
        two = (int)change / 2;
        change = change % 2;
    }

    else if (change >= 1)
    {
        one = (int)change / 1;
        change = change % 1;
    }
    else if (change > 1)
    {
        decimal fhu = change / 0.5m;
        centsfifty = (int)fhu;
        change = change % 0.5m;
        Console.WriteLine("YOUR CHANGE IS:");
    }

    } while (change >= 0);


    Console.WriteLine("YOUR CHANGE IS:");
    Console.WriteLine("---------------");
    Console.WriteLine("HUNDREDS RANDS \t:  {0}", hundreds);
    Console.WriteLine("FIFTY RANDS \t:  {0}", fifty);
    Console.WriteLine("TWENTY RANDS \t:  {0}", twenty);
    Console.WriteLine("TEN RANDS \t:  {0}", ten);
    Console.WriteLine("FIVE RANDS \t:  {0}", five);
    Console.WriteLine("TWO RANDS \t:  {0}", two);
    Console.WriteLine("ONE RANDS \t:  {0}", one);
    Console.WriteLine("50 CENTS \t:  {0}", centsfifty);

}
}
}
4

3 回答 3

1

帮助简化代码并提高其可读性的一种方法是消除条件和嵌套算术运算,这几乎总是使代码更难理解。

我看到 int 被施放了太多次,这可能意味着最好从一开始就使用 int 。即使您将 cost 和 cashreceived 变量声明为十进制类型,它们都被强制转换为 int。因此,最好从一开始就将它们都声明为 int,这将有助于简化转换。

你可以做这样的事情

hundreds = change / 100;
change %= 100;

fifty = change / 50;
change %= 50;

ten = change / 10;
change %= 10;

five = change / 5;
change %= 5;

two = change / 2;
change %= 2;

one = change;
于 2014-02-16T02:51:41.860 回答
1

您可以使用一种模式来获得这样的金额,

这是一个帮助您入门的小示例,您可能可以将其包装在一个函数或其他东西中,但它可以让您了解从哪里开始。

// number to find values from
int change = 254;

int _hundreds = 100;
int _fifty = 50;
int _twenty = 20;
int _ten = 10;
int _five = 5;
int _two = 2;
int _one = 1;

int hundreds = (int)(change / _hundreds);
int fifty = (int)((change % _hundreds) / _fifty);
int twenty = (int)(((change % _hundreds) % _fifty) / _twenty);
int ten = (int)((((change % _hundreds) % _fifty) % _twenty) / _ten);
int five = (int)(((((change % _hundreds) % _fifty) % _twenty) % _ten) / _five);
int two = (int)((((((change % _hundreds) % _fifty) % _twenty) % _ten) % _five) / _two);
int one = (int)(((((((change % _hundreds) % _fifty) % _twenty) % _ten) % _five) % _two) / _one);

退货

hundreds = 2
fifty = 1
twenty = 0
ten = 0
five = 0
two = 2
one = 0;
于 2013-02-11T20:51:12.467 回答
0

代替

     if (change >= 100)
     {
     hundreds = (int)change / 100;
     change = (int)change % 100;

     }

尝试使用

        if (change >= 100)
        {
            decimal rand = change / 100;
            hundred= (int)rand;
            change = change % 100;
        }


        if (change >= 50)
        {
            decimal rand = (int)change / 50;
            fifty = (int)rand;
            change = change % 50;
        }


        if (change >= 20)
        {
            decimal rand = change / 20;
            twenty = (int)rand;
            change = change % 20;
        }

我希望这有帮助。

于 2013-11-01T21:36:30.170 回答