1 halves
1 quarter
0 dimes
0 nickels
0 pennies
My problem is that with the method that I wrote if i put in say 30 it will give me an output of:
1 halves
1 quarters
0 dimes
0 nickels
0 pennies
As I step through my code I see that for halves even though it doesn't get any it will still give the first (1) and output it to the halves spot. What am I missing? Any hints?
Also most of the code was provided for us the only part we needed to fill in was the method.
using System;
class Program
{
// some class level constants
const int HALVES = 50;
const int QUARTERS = 25;
const int DIMES = 10;
const int NICKELS = 5;
const int PENNIES = 1;
static void Main()
{
int money; // the value we want to count change for
Console.WriteLine("I will make change for you.");
Console.Write("Enter in an amount between 1 and 99: ");
money = int.Parse(Console.ReadLine( ) );
Console.WriteLine("For {0} you get:", money);
Console.WriteLine("{0} halves", ComputeChange(ref money, HALVES) );
Console.WriteLine("{0} quarters", ComputeChange(ref money, QUARTERS) );
Console.WriteLine("{0} dimes", ComputeChange(ref money, DIMES) );
Console.WriteLine("{0} nickels", ComputeChange(ref money, NICKELS) );
Console.WriteLine("{0} pennies\n", ComputeChange(ref money, PENNIES) );
Console.ReadLine( );
}
// The ComputeChange Method
// you provide the method to compute change in the space below
// Purpose: This method figures out the amount of coins you need starting largest to smallest,
// Inputs: The method takes two imputs changeValue and coinValue.
// Returns: Coins needed
// Preconditions: Start at the largest coin (hald dollar) work down to smalles (penny).
// Postconditions: The return value must be the full change amount in coins.
// -----------------------------------------------------------
static int ComputeChange(ref int changeValue, int coinValue)
{
//compute the .50 pieces
if (changeValue >= HALVES)
{
coinValue = changeValue / HALVES;
//compute new value after .50 pieces
changeValue = changeValue - (coinValue * HALVES);
}
//compute the .25 pieces
else if (changeValue >= QUARTERS)
{
coinValue = changeValue / QUARTERS;
//compute the value after .25 pieces
changeValue = changeValue - (coinValue * QUARTERS);
}
//compute the .10 pieces
else if (changeValue >= DIMES)
{
coinValue = changeValue / DIMES;
//compute the value after .10 pieces
changeValue = changeValue - (coinValue * DIMES);
}
//compute the .05 pieces
else if (changeValue >= NICKELS)
{
coinValue = changeValue / NICKELS;
//compute the value after .05 pieces
changeValue = changeValue - (coinValue * NICKELS);
}
//compute the .01 pieces
else
{
coinValue = changeValue / PENNIES;
}
//compute the value after .01 pieces
return coinValue;
}
}

New Topic/Question
Reply




MultiQuote



|