The Artima Developer Community
Sponsored Link

Java Answers Forum
Average

2 replies on 1 page. Most recent reply: Nov 27, 2003 3:43 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
bob

Posts: 1
Nickname: squeaky
Registered: Nov, 2003

Average Posted: Nov 25, 2003 8:01 PM
Reply to this message Reply
Advertisement
I have a program where i'm supposed to get the sum and the average depending on how many inputs the user has. The program stops after a 0 is entered. I got it to figure the sum, just can't get it to figure the average.


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Average Posted: Nov 26, 2003 5:25 AM
Reply to this message Reply
Just count the inputs.
If your input Command is not in the method wich builds the sum, use a static variable wich you increment after the input, if the input was not 0.
At the end, do something like sum / java.lang.Math.max (1, nInputs)
I used the max method here, in case you don't want the last 0 to be used for the

The code without a static variable would look like this:

int counter = 0
double sum = 0;
do
{
double val = input(); //the input text gets converted to double/float/int inside the input-Method.
sum += val;
if (val != 0)
counter++;
} while (val != 0);
double average = sum / java.lang.Math.max (1, counter);




This REALLY isn't a Java problem, it's just basic programming.

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Average Posted: Nov 27, 2003 3:43 PM
Reply to this message Reply
http://www.quantumhyperspace.com/JavaProgramming/JavaSourceBank/viewCode.jsp?javaFile=Tally.java
or

import java.io.*;
 
public class Tally{
 
    public static void main(String[] args){
        try{
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);
            String nextLine = "";
            double sum = 0;
            double average = 0;
            int entries = 0;
            System.out.println("Enter a number or 0 to quit."); 
            while (!(nextLine = br.readLine()).equals("0")){
                try{
                    double nextNumber = Double.parseDouble(nextLine);
                    sum = sum + nextNumber;
                    entries++;
                    if (entries > 0) average = sum/entries;
                    System.out.println("Entry: " + String.valueOf(nextNumber) 
                        + "\tSum: " + String.valueOf(sum)
                        + "\tAverage: " + String.valueOf(average));
                }catch(NumberFormatException nfe){
                    System.err.println("That entry could not be converted to a number. Try again.");
                }
                System.out.println("Enter a number or 0 to quit."); 
            }
            System.out.println("Sum: " + String.valueOf(sum)
                        + "\tAverage: " + String.valueOf(average));
        }catch(IOException ioe){
            System.err.println("Uh oh");
        }
 
    }
 
 
}

Flat View: This topic has 2 replies on 1 page
Topic: how to calculate arctan( ) of a function Previous Topic   Next Topic Topic: Database

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use