LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-02-2010, 06:31 PM   #1
xskycamefalling
Member
 
Registered: May 2009
Posts: 43

Rep: Reputation: 15
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);
		
	}
}
 
Old 09-02-2010, 06:41 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] [BASH] non-empty variable before loop end, is empty after exiting loop aitor Programming 2 08-26-2010 09:57 AM
write line count to a variable? tatoosh Linux - Newbie 9 07-30-2009 03:44 PM
problem with setting CLASS_PATH variable for compile java karthikeyhegde Linux - General 3 04-19-2007 01:45 AM
java: inner class, variable accessibility problem nadroj Programming 2 10-05-2006 09:18 PM
getting a variable out of a WHILE loop. fhinkle Programming 8 02-22-2005 04:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration