the program works and all but i dont want my qts,dms,nks,pns to be in a decimal form i keep changing it but then it doesnt wotk can anybody help me pleasee i really appreciate it am not sure how to improve it..thank you so much....

import java.util.Scanner;
public class Program1
{
public static void main(String [] args)
{
double purchase,change,quarters,dimes,nickels,pennies;
Scanner keyboard=new Scanner(System.in);
System.out.println("Enter purchase amount under 1.00:");
purchase=keyboard.nextDouble();

change = 100 - purchase;
quarters = (change / 25);
dimes = (change / 10);
nickels = (change / 5);
pennies = (change / 1);

System.out.println("Enter purchase amount: " + purchase);
System.out.println("The change is: " + change);
System.out.println("The number of quarters is: " + quarters);
System.out.println("The number of dimes is: " + dimes);
System.out.println("The number of nickels is: " + nickels);
System.out.println("The number of pennies is: " + pennies);
}
}

Recommended Answers

All 7 Replies

javaAddict had the write idea, unfortunately, your variables are doubles, so you can't simply cast the division operations to ints. You can, however, do this in the print statements.

I.E.

System.out.println("Enter purchase amount: " + purchase);
System.out.println("The change is: " + change);
System.out.println("The number of quarters is: " + (int) quarters);
System.out.println("The number of dimes is: " + (int) dimes);
System.out.println("The number of nickels is: " + (int) nickels);
System.out.println("The number of pennies is: " + (int) pennies);

Edit: The better solution, however, is javaAddicts, combined with you changing the quarters, dimes, nickels, and pennies to ints, rather than doubles.

Edit Again: And I see, he mentioned that also, you seemingly, simply didn't do it. Try javaAddicts solution, and if it doesn't work, post your modified code here with the errors.

> javaAddict had the write idea

Write? ;-)

I have a question though! With reference to the above given code, if I typecast doubles to int as follows:

double d = 0.8;
int i = (int)d;

..the double value for 0.8 is converted to 0 as integer. Wouldn't it be better to write the code as:

...
int i = (int)Math.round(d);

.. so that the returned interger value would be the proper whole number for every double value.

Not in his case, if the change is 0.38, he would want dimes to equal 3, not 4.

> javaAddict had the write idea

Write? ;-)

Yep. He rote write. ;-)

(He wrote right, of course.)

thanks alot everybody i really appreciate it
THANK YOU!!!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.