Jump to content

my double is funny

- - - - -

  • Please log in to reply
3 replies to this topic

#1
agnl666

agnl666

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
  • Programming Language:C, Java, C++
  • Learning:Python, Assembly
I'm writing a program to average given inputed numbers. The problem I'm having is something I have never encountered. No matter what my answer is returned as #.0 (# can be subbed for any given number). My main looks as fallows. (I don't know the tag for posting code)

public static void main(String [] args)
{
    displayHeader();        

    Scanner in = new Scanner(System.in);
    double sum, price;
    int count = 0;
    sum = 0.0;
            
    System.out.println("Enter the price of gas or -99 to Quit: ");
    price = in.nextDouble();
    while (price != -99)
    {
        price = in.nextDouble();
        if (price != -99) 
        {
            sum += price;
            count++;
        }            
    }
    sum /= count;
    System.out.println("\nThe average price per gallon of regular gas is " + sum);
}

So I was wondering at what point I'm losing my decimals. I've tried changing things up but I'm at a loss. Thank you in advance to anyone who lends me a hand.

Edited by Alexander, 22 November 2010 - 10:45 PM.
[code]in here[/code]


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
You accepted input of price before the while loop, and then again right away inside of it, this discards the first number and makes your count wrong.

I also put a printline to debug information, and print the variables to the console. This is useful for finding bugs, you would see the sum and count was incorrect in your program.
import java.util.Scanner;

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

        Scanner in = new Scanner(System.in);
        double sum, price = 0;
        int count = 0;
        sum = 0.0;

        System.out.println("Enter the price of gas or -99 to Quit: ");
        while (price != -99)
        {
            price = in.nextDouble();
            if (price != -99)
            {
                sum += price;
                count++;
            }
        }
        System.out.println("\nSum: " + sum + "\nCount: " + count + "\n" );

        sum /= count;
        System.out.println("\nThe average price per gallon of regular gas is " + sum);
    }
}

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Prints with correct decimals here..

#4
agnl666

agnl666

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
  • Programming Language:C, Java, C++
  • Learning:Python, Assembly
thank you. i understand how i'm losing the first given value of price though i don't understand how that is giving me a lack of decimal unless it is purely by chance that the list of numbers i am giving it with out the first number returns a whole number. thank you, i intend to check it out.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users