简体   繁体   中英

calculating max and average of input numbers

this is my code

import java.util.*;
public class testq
{
    public static void main (String [] args)
    {
        int max = 0;
        int sum = 0;
        int count = 0;
        int age, maximum;
        double average;

        Scanner sc = new Scanner(System.in);
        age = sc.nextInt();
        while (age != 0)
        {
            System.out.println("enter age");
            age = sc.nextInt();

            if (age < 0 && age > 120)
            {
                System.out.println("enter numbers between 1 to 120");
                age = sc.nextInt();
            }

            else
            {
                count ++;
                sum = sum + age;
                maximum = getMax(max, age);
                average = getAve(sum, count);
            }
        }
        System.out.println(" max is" + max + "average is" + average);

    }

    public static int getMax (int max, int age)
    {
        if (max < age)
        {
            max = age;
        }
        return max;
    }

    public static double getAve (int sum, int count)
    {
        double average;
        average = (double)sum / (double)count;
        return average;
    }
}

this is what i have i need to input values between 1 to 120 until 0 is input and calculate max and average of those values if i compile this code i get error message

"average might not have been initialised"

i was thinking maybe somethings wrong with the count so the method for calculating average might not have executed properly. i cant think of anything else than that at the moment could i get help what went wrong please?

edit*

my code looks like this now

import java.util.*;
public class testq 
{
    public static void main (String [] args)
    {
        int max = 0;
        int sum = 0;
        int count = 0;
        int age, maximum;
        double average;

        Scanner sc = new Scanner(System.in);
        System.out.println("enter age");
        age = sc.nextInt();
        while (age != 0)
        {
            System.out.println("enter age");
            age = sc.nextInt();

            if (age < 0 || age > 120)
            {
                System.out.println("enter numbers between 1 to 120");
                age = sc.nextInt();
            }

            else
            {
                count ++;
                sum = sum + age;
            }
        }
        maximum = getMax(max, age);
        average = getAve(count, sum);
        System.out.println(" maximum is " + max + "average is " + average);

}

but the problem now is max is always 0 and average is always close to 0. also when i input numbers it takes in the negative values as well for example

enter age
-10
enter age
-10
enter numbers between 1 to 120
-10
enter age
-10
enter numbers between 1 to 120
0

i wanted to out put "enter numbers between 1 to 120 everytime invalid number is input

ive tried changing the while condition to (age != 0 && age >=1 && age <=120) but didnt work

It is possible for 0 to be entered immediately and average would never get set. Therefore it would be an error when you tried to print it. You could move your call to getAve() right before the print statement (and out of the while loop) since you should only have to call it once.

    average = getAve(sum, count);
    System.out.println(" max is " + max + " average is " + average);

Also, I am guessing that you should use an "or" in your condition instead of an "and".

if (age < 0 || age > 120)

instead of:

if (age < 0 && age > 120)

It would be impossible for age to be less than zero and greater than 120.

The value for average is not set in every logical path through your main() method, hence the warning.

You should probably initialise it when you declare it, eg:

double average = 0.0;

Alternatively, you could take advantage of the fact you do not need to re-calculate the average every time though your while loop. If you declare and calculate it just once, after the loop, the value will always be set.

You have a method

public static double getAve (int sum, int count)

but you use

average = getAve(count, sum);

so your arguments are in reverse order.

Second problem is that you use max here

maximum = getMax(maximum, age);

and here

System.out.println(" maximum is " + max

but your real value is maximum , so switch that too.

Please use this code this will produce your required output,

public static void main(String[] args) {

        //int max = 0;
        int sum = 0;
        int count = 0;
        int age, maximum=0;
        double average= 0;

        Scanner sc = new Scanner(System.in);
        do {
            System.out.println("enter age");
            age = sc.nextInt();

            if (age <0||age > 120)
            {
                System.out.println("enter numbers between 1 to 120");
                //age = sc.nextInt();
            }

            else
            {
                count ++;
                sum = sum + age;
                maximum = getMax(maximum, age);
                                }
        }while(age != 0);
        average = getAve(sum, count);
        System.out.println("max is" + maximum + "\naverage is" + average);

    }

    public static int getMax (int max, int age)
    {
        if (max < age)
        {
            max = age;
        }
        return max;
    }

    public static double getAve (int sum, int count)
    {
        double average;
        average = (double)sum / (double)count;
        return average;
    }

Output:

enter age
3
enter age
6
enter age
3
enter age
120
enter age
121
enter numbers between 1 to 120
enter age
500
enter numbers between 1 to 120
enter age
6
enter age
0
 max is120
average is23.0

The following changes were made in your code,

  • do while loop is used instead of while loop.
  • You pass max as argument for getMax() but you need to pass maximum as an argument.
  • Double is initialized as 0.

If you have any query please comment below.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM