2013-02-11 73 views
-1

我有一個象牙,我被要求編寫一個程序,計算現金交易後給客戶的變化,並確定每個面額的鈔票和硬幣的數量。更改計算器

用戶必須投入貨物成本和從客戶收到的金額。

我必須有一個接受小數參數Cost和Chashreceived以及整數參數的方法:Hunderds,Fifties,Twisted,Ones,50c,10c,5c,2c和1c。

我減去成本和Cashreceived並計算需要作爲變更返回的紙幣和硬幣的確切數量。

我已經嘗試過了,但是當我必須放入硬幣時,它會變得有問題。

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); 

} 
} 
} 
+0

。 – jeuton 2013-02-11 20:22:19

+0

不應該最後'else if'爲'else if(更改<1)'?我認爲你的標誌翻轉了。 – IronMan84 2013-02-11 20:22:49

+3

建議 - 不要在代碼中使用像cls這樣的前綴。 「現金」對於課程名稱來說足夠了。 IDE會告訴你關於類型 – 2013-02-11 20:22:52

回答

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; 
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; 
     } 

我希望這有助於。

0

有助於簡化代碼並提高可讀性的一種方法是消除條件和嵌套算術運算,這幾乎總是使代碼更難理解。

我看到int被投了太多次,這可能意味着最好從頭開始使用int。儘管您將成本和現金收入變量聲明爲十進制類型,但它們都被轉換爲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;