need help with some basic java.

Posted by Racket on Stack Overflow See other posts from Stack Overflow or by Racket
Published on 2010-12-26T20:42:32Z Indexed on 2010/12/26 20:54 UTC
Read the original article Hit count: 137

Filed under:
|

Hi,

I'm doing the first chapter exercises on my Java book and I have been stuck for a problem for a while now. I'll print the question,

prompt/read a double value representing a monetary amount. Then determine the fewest number of each bill and coin needed to represent that amount, starting with the highest (assume that a ten dollar bill is the maximum size needed). For example, if the value entered is 47,63 (forty-seven dollars and sixty-three cents), and the program should print the equivalent amount as:

  1. 4 ten dollar bills
  2. 1 five dollar bills
  3. 2 one dollar bills
  4. 2 quarters
  5. 1 dimes
  6. 0 nickels
  7. 3 pennies"

etc.

I'm doing an example exactly as they said in order to get an idea, as you will see in the code. Nevertheless, I managed to print 4 dollars, and I can't figure out how to get "1 five dollar", only 7 dollars (see code).

Please, don't do the whole code for me. I just need some advice in regards to what I said. Thank you.

import java.util.Scanner;

public class PP29 {
    public static void main (String[] args) {

        Scanner sc = new Scanner (System.in);

        int amount;
        double value;
        double test1;
        double quarter;

        System.out.println("Enter \"double\" value: ");
        value = sc.nextDouble();

        amount = (int) value / 10;      // 47,63 / 10 = 4. 
        int amount2 = (int) value % 10; // 47 - 40 = 7

        quarter = value * 100;          // 47,63 * 100 = 4736
        int sum = (int) quarter % 100;  // 4763 / 100 => 4763-4700 = 63.

        System.out.println(amount);
        System.out.println(amount2);
    }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about math