LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   java problem within while loop and count variable.. (https://www.linuxquestions.org/questions/programming-9/java-problem-within-while-loop-and-count-variable-830039/)

xskycamefalling 09-02-2010 06:31 PM

java problem within while loop and count variable..
 
i have a program that breaks up the ammount of change given into the corosponding number of quarters dimes nickels and pennies.. anyway for some reason im not getting the correct number of pennies its off by one.. every time except for the input .07 this gives the correct ammount but .08 gives the same as .07
Code:

import java.util.*;
public class Lab3
{
        public static void main(String[] args)
        {
               
                final double QUARTERS = .25;
                final double DIMES = .10;
                final double NICKELS = .05;
                final double PENNIES = .01;
                int quarters_Count = 0;
                int dimes_Count = 0;
                int nickels_Count = 0;
                int pennies_Count = 0;
                double ammount;
                Scanner console = new Scanner(System.in);

                System.out.println("Enter the ammount of money ");
                ammount = console.nextDouble();
                System.out.printf("\nThe ammount you entered was: %.2f \n", ammount);
               

                while (true)
                {
                        System.out.printf("\n: %.2f \n", ammount);
                        if (ammount >= QUARTERS)
                        {
                                ammount = ammount - QUARTERS;
                                quarters_Count ++;
                        }
                        else if (ammount >= DIMES)
                        {
                                ammount = ammount - DIMES;
                                dimes_Count ++;
                        }
                        else if (ammount >= NICKELS)
                        {
                                ammount = ammount - NICKELS;
                                nickels_Count ++;
                        }
                        else if (ammount >= PENNIES)
                        {
                                pennies_Count += 1;
                                ammount = ammount - PENNIES;
                                //pennies_Count ++;
                        }
                        else
                                break;
                }
                       
               
                System.out.printf("\nQuarters: %3d \nDimes: %6d \nNickels: %4d \nPennies: %4d \n", quarters_Count, dimes_Count, nickels_Count, pennies_Count);
               
        }
}


Sergei Steshenko 09-02-2010 06:41 PM

Floating point arithmetic is inexact. You are dealing with periodic fractions - even if think you don't - because your computer works in radix 2 and not in radix 10.

Before ever using floating point in whatever programming language read and understand http://en.wikipedia.org/wiki/Floating_point .

Again, your problem has nothing to do with Java. To resolve your problem deal with integers only, i.e. scale up everything by 100.


All times are GMT -5. The time now is 12:15 PM.