2016-05-15 95 views
0

明天我正在檢查我的測試..我在程序中遇到了一個問題(我需要創建一個程序來顯示輸入數量的細目..我有與仙一個問題...)有關在C中崩潰的程序#

Console.Write("Enter amount: "); 
double amt = double.Parse(Console.ReadLine()); 

thou = (int)amt/1000; 
change = (int)amt % 1000; 

fivehun = (int)change/500; 
change = change % 500; 

twohun = (int)change/200; 
change = change % 200; 

hun = (int)change/100; 
change = change % 100; 

fifty = (int)change/50; 
change = change % 50; 

twenty = change/20; 
change = change % 20; 

ten = (int)change/10; 
change = change % 10; 

five = (int)change/5; 
change = change % 5; 

one = (int)change/1; 
change = change % 1; 

twencents = (int)(change/.25); 
change = change % .25; //there was an error here.. starting here 

tencents = (int)(change/.10); 
change = change % .10; 

fivecents = (int)(change/.05); 
change = change % .05; 

onecent = (int)(change/.01); 
change = change % .01; 

Console.WriteLine("The breakdown is as follows: "); 
Console.WriteLine("Php 1000   ={0} ", thou); 
Console.WriteLine("Php 500   ={0} ", fivehun); 
Console.WriteLine("Php 200   ={0} ", twohun); 
Console.WriteLine("Php 100   ={0} ", hun); 
Console.WriteLine("Php 50   ={0} ", fifty); 
Console.WriteLine("Php 20   ={0} ", twenty); 
Console.WriteLine("Php 10   ={0} ", ten); 
Console.WriteLine("Php 05   ={0} ", five); 
Console.WriteLine("Php 01   ={0} ", one); 
Console.WriteLine("Php 0.25   ={0} ", twencents); 
Console.WriteLine("Php 0.10   ={0} ", tencents); 
Console.WriteLine("Php 0.05   ={0} ", fivecents); 
Console.WriteLine("Php 0.01   ={0} ", onecent);    

Console.ReadKey(); 

錯誤說我不能轉換雙爲int,所以我試着將它轉換我的鑄造它

change = (double) change % .25; 

仍然是一個錯誤..

+0

您是否嘗試將'.25'改爲'.25f'? –

+0

是仍然不能正常工作.. – Franchette

+0

看起來模數表達式的兩邊必須是'相同類型'。或者你需要自己重載它。請參閱:https://msdn.microsoft.com/en-US/library/0w4e0fzs(v=VS.100).aspx –

回答

0

EDITED

最初使雙變= 0和和分裂AMT到2個變量

double wholeValues = (int)amt; 
double decimalValues = amt - wholeValues; 

輸入然後改變

thou = (int)amt/1000; 
change = (int)amt % 1000; 

使其作爲

thou = (int)wholeValues/1000; 
change = (int)wholeValues % 1000; 

否則你將在這一點上

捨去小數值,但你缺少一個施放1在

twenty = (int) change/20; 

模塊爲int將再次給出相同的價值,用新的變量decimalValues開始美分計算

one = (int)change/1; 

change = decimalValues * 100; 

twencents = (int)(change/25); 
change = change % 25; 

tencents = (int)(change/10); 
change = change % 10; 

fivecents = (int)(change/5); 
change = change % 5; 

如果我們使用十進制值模塊,你有時可能會與例如不正確的值 結束了。30美分,它將代表0.25美分= 1,0.05美分= 0, 0.01美分= 4

+0

當刪除(int)錯誤時加了.. – Franchette

+0

可以指定其他錯誤嗎? – CloudSL

+0

不能將類型'double'轉換爲'int'顯式轉換存在(是否缺少一個轉換?)這是錯誤,但是當我做了你告訴我的時候..另一個錯誤就像添加到列表 – Franchette

1

使用做uble change = 0;而不是int change = 0;

+0

當我做了更多錯誤時那.. – Franchette

0

終於明白了!

int thou, fivehun, twohun, hun, fifty, twenty, ten, five, one; 
double change = 0; // added this one as suggested 

Console.Write("Enter amount: "); 
double amt = double.Parse(Console.ReadLine()); 


thou = (int)amt/1000; 
change = amt % 1000; //remove the int (change should be double) 

fivehun = (int)change/500; 
change = change % 500; 

twohun = (int)change/200; 
change = change % 200; 

hun = (int)change/100; 
change = change % 100; 

fifty = (int)change/50; 
change = change % 50; 

twenty = (int) change/20; //added int here 
change = change % 20; 

ten = (int)change/10; 
change = change % 10; 

five = (int)change/5; 
change = change % 5; 

one = (int)change/1; 
change = change % 1; 

int twencents = (int)(change/0.25); 
change = change % 0.25; 

int tencents = (int)(change/0.10); 
change = change % 0.10; 

int fivecents = (int)(change/0.05); 
change = change % 0.05; 

int onecent = (int)(change/0.01); 
change = change % 0.01;