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");
             }



           }           
    }

You ask rhe user for a number between 0 and 1.00, but you read his reply as an int - which has no decimal places

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.