這程式內容沒有什麼特別的地方,但是如果用一堆if判斷式會寫到瘋掉.. :P
if (iReturn < 0)
{
MessageBox.Show("錢不夠! 再給我");
return;
}
if (iReturn == 0) //如果應找的錢等於0的話,就不需藥再繼續執行,直接結束
{
MessageBox.Show("不用找錢");
return;
}
int[] MoneyTypes = { 2000,1000,500,200, 100 ,50,10,5,1};
string strMessage = "找零數目:";
iReturn = Math.Abs(iReturn); //這是確保找錢金額為 正數..沒有負數
for (int i = 0; i < MoneyTypes.Length; i++)
{
int iNum = 0;
while ((iReturn - MoneyTypes[i]) >= 0)
{
iNum = iNum + 1;
iReturn = iReturn - MoneyTypes[i];
}
switch (i)
{
case 0: // 2000
strMessage = strMessage + " 2000元 x " + iNum + "張\t";
break;
case 1: //1000
strMessage = strMessage + " 1000元 x " + iNum + "張\t";
break;
case 2: // 500
strMessage = strMessage + " 500元 x " + iNum + " 張\t";
break;
case 3: // 200
strMessage = strMessage + " 200元 x " + iNum+ " 張\t";
break;
case 4: // 100
strMessage = strMessage + " 100元 x " + iNum+ " 張\t";
break;
case 5: // 50
strMessage = strMessage + " 50元 x " + iNum + " 張\t";
break;
case 6: // 10
strMessage = strMessage + " 10元 x " + iNum+ " 張\t";
break;
case 7: // 5
strMessage = strMessage + " 5元 x " + iNum+ " 張\t";
break;
case 8: // 1
strMessage = strMessage + " 1元 x " + iNum+ " 張\t";
break;
}
}
strMessage 就是找錢出來的結果.
沒有留言:
張貼留言