0

Im trying to take an amount of change, less than $1.00, but more than zero and display the number of quarters, dimes, nickels, and pennies that will be required to make that change. The output must be presented in a descriptive manner that resembles the English language with the following gramatical rules:

if the amount is zero, the word "no" should be used. if the amount is 1, the unit must be singular. if the amount is not 1, the unit must be plural.

Also, The program should catch a user mistake when entering anything under 0 or over 1 and go into a data validation loop to display the prompt and ask that the user re-enter the amount. This loop would continue until the user enters an amount less than $1.00, but more than zero.

When I run my code I kept getting 4 repeatedly.

Thanks for your time! and any help is very much appreciated!

My code so far:

import java.util.Scanner;

    public class CoinChange
    {
        public static void main(String[] args)
        {               
           double amt; 
           int cents, quarter, dime, nickle, penny;

           Scanner keyboard = new Scanner(System.in); 

           System.out.print("Change in Coins\n" +
                           "----------------\n\n");

           System.out.println("Enter the amount less than $1.00, but\n" +
                            "more than zero.");

           System.out.print("\nEnter amount: ");
                    amt = keyboard.nextInt();

    // ----------------------------------------------             

           cents = (int)(amt*100 + .1);

           quarter = cents/25;
           cents %= 25;

           dime = cents/10;
           cents %= 10;

           nickle = cents/5;
           cents %= 5;

           penny = cents;

    // -----------------------------------------

           while(amt > 0 && amt <= 1)
           {

    // ----------------------------------------- 

             if(quarter == 0)
             {
                System.out.print("no ");
             }
             else
                System.out.print(quarter);
             }
             if(quarter == 1)
             {
                System.out.print("quarter");
             }
             else
             {
                System.out.print("quarters");
             }



           }           
    }
2
Contributors
1
Reply
12
Views
4 Years
Discussion Span
Last Post by JamesCherrill
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.