开发者

Breaking down currency in c#

开发者 https://www.devze.com 2022-12-26 05:43 出处:网络
i\'m trying to get this program to break down a user defined amount of dollars into the fewest possible bills. i don\'t think my for loops are running because if i put a writeline line in them it does

i'm trying to get this program to break down a user defined amount of dollars into the fewest possible bills. i don't think my for loops are running because if i put a writeline line in them it doesn't show up when i run it.

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

namespace ConsoleApplication13
{
    class Program
    {

        static void Main(string[] args)
        {
            Console.Write("Enter the amount of money: $");
            int totalAmount = Convert.ToInt32(Console.ReadLine());
            calculateNumberOfBills(totalAmount);
        }

        static void calculateNumberOfBills(int totalAmount)
        {

            int[] denominations = { 20, 10, 5, 1 };
            int[] numberOfBills = new int[4];
            for (numberOfBills[0] = 0; totalAmount < 20; numberOfBills[0]++)
            {
                totalAmount = totalAmount - 20;
            }
            for (numberOfBills[1] = 0; totalAmount < 10; numberOfBills[1]++)
            {
                totalAmount = totalAmount - 10;
            }
            for (numberOfBills[2] = 0; totalAmount < 5; numberOfBills[2]++)
            {
                totalAmount = totalAmount - 5;
            }
            for (numberOfBills[3] = 0; totalAmount <= 0; numberOfBills[3]++)
            {
                totalAmount = totalAmount - 1;
            }
            Console.WriteLine("Number of twenties" + numberOfBills[0]);
            Console.WriteLine("Number of tens" + numberOfBills[1]);
            Console.WriteLine("Number of fives" + numberOfBills[2]);
            Console.W开发者_运维知识库riteLine("Number of ones" + numberOfBills[3]);
        }
    }
}


Take a look at this:

for (numberOfBills[0] = 0; totalAmount >= 20; numberOfBills[0]++)
    {
        totalAmount = totalAmount - 20;
    }
    for (numberOfBills[1] = 0; totalAmount >= 10; numberOfBills[1]++)
    {
        totalAmount = totalAmount - 10;
    }
    for (numberOfBills[2] = 0; totalAmount >= 5; numberOfBills[2]++)
    {
        totalAmount = totalAmount - 5;
    }
    for (numberOfBills[3] = 0; totalAmount > 0; numberOfBills[3]++)
    {
        totalAmount = totalAmount - 1;
    }


This is a homework question, right?

        for (numberOfBills[0] = 0; totalAmount < 20; numberOfBills[0]++)

make it

        for (numberOfBills[0] = 0; totalAmount >= 20; numberOfBills[0]++)

and try again :) The center piece is the condition where the loop should run.


Sorry, at this point this is "not a real question" and should most probably be tagged as homework.

Your "smaller than" comparisons should be changed to "greater than" comparisons and you're good to go with your solution. What's happening now is an integer overflow, eventually causing your first for-loop , which was infinitely looping until then to break.

There are easier ways to solve your problem, try it with a single loop and the modulo operator).


in your "for" loops you have inverse condition => totalAmount < 20 means that it executes loop while totalAmount is less than 20 - that is the opposite of what you want.

Change it to

for(...;totalAmount > 20; ...)


Try this, a bit of less code

    int amt = 73;

    Dictionary<int, int> dic = new Dictionary<int, int>() {{20,0},{10,0},{5,0},{1,0}};
    int[] keys =new  int[dic.Count];
    dic.Keys.CopyTo(keys, 0);

    foreach (int i in  keys)
    {            
        if (amt >= i)
        {
            dic[i] = amt / i;
            amt = amt % i;
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号