Need help with this program
import java.util.Scanner;
public class PP_2_11a
{
//---------------------------------------------------------------------
// Reads amount of money and determines what bills and coins are needed
//---------------------------------------------------------------------
public static void main (String[]args)
{
double total_amount;
Scanner scan = new Scanner (System.in);
boolean more = true;
while (more)
{
System.out.println ("Enter amount of money.");
total_amount = scan.nextDouble();
int tens = (int)total_amount/10;
total_amount = total_amount%10;
int fives = (int)total_amount/5;
total_amount = total_amount%5;
int ones = (int)total_amount/1;
total_amount = total_amount%1;
int quarters = (int)(total_amount/.25);
total_amount = (total_amount%.25);
int dimes = (int)(total_amount/.10);
total_amount = (total_amount%.10);
int nickels = (int)(total_amount/.05);
total_amount = (total_amount%.05);
int pennies = (int)(total_amount%.01);
System.out.println (tens + " ten dollar bills");
System.out.println (fives + " five dollar bills");
System.out.println (ones + " one dollar bills");
System.out.println (quarters + " quarters");
System.out.println (dimes + " dimes");
System.out.println (nickels + " nickels");
System.out.println (pennies + " pennies");
System.out.print ("Enter 1 to continue, 0 to quit > ");
more = (1 == scan.nextInt());
}
}
}
it does not calculate properly when I input the amount 68.97
please help
thank you!
Re: Need help with this program
Quote:
it does not calculate properly when I input the amount 68.97
Please explain what the code should do.
Copy and paste here the contents of the console screen from when you run the program
Add some comment to the output saying what the output should be.
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'
Paste here.
There are some problems working with floating point numbers.
Read this:http://download.oracle.com/docs/cd/E..._goldberg.html
One trick is to convert the amount to cents and scale your divisors accordingly.
Re: Need help with this program
my program needs to be able to read the amount of money and then output how many tens, fives, ones, quarters, dimes and so on that are needed to make the amount.
When I run the program it does everything right except it does not calculate the amount of pennies right:
----jGRASP exec: java PP_2_11a
Enter amount of money.
68.97
6 ten dollar bills
1 five dollar bills
3 one dollar bills
3 quarters
2 dimes
0 nickels
1 pennies
Enter 1 to continue, 0 to quit >
there should be 2 pennies but I do not understand why it is not calculating it right.
Re: Need help with this program
Add some printlns in your code to print out the intermediate values so you can see what is happening.
Re: Need help with this program
I was able to find out where it went wrong when I put in the printlns-thank you!