1

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

cheong kim
  • 21
  • 1
  • 7

4 Answers4

2

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.

jconra
  • 84
  • 4
  • Karthi's answer should work for you. I didn't mean for you to take your call to getMax() out of the while loop. It needs to be in your loop to work. I still think it is a good idea to have getAve() right before the print statement since it only needs to be called once. It looks like when you gave it -10 it didn't take it because it followed the path that printed "enter numbers between 1 to 120". Therefore it didn't increase count or sum. One thing that can help solve problems is to print the count and sum every time it loops until it is working. – jconra Sep 19 '16 at 06:26
1

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.

dave
  • 11,641
  • 5
  • 47
  • 65
1

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.

Mark
  • 2,167
  • 4
  • 32
  • 64
0

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.

Karthikeyan KR
  • 1,134
  • 1
  • 17
  • 38
  • You are calculating the average each iteration, better to do it once at the end. – Mark Sep 19 '16 at 06:29
  • np, what is `int max = 0;` for? – Mark Sep 19 '16 at 06:41
  • Ha ha , its already in his code. I just modified his code. I didn't check whether an extra variable is present or not. Thanx for noting it too. – Karthikeyan KR Sep 19 '16 at 06:54
  • thank you so much for changing my code it works perfectly now, but im not really sure the whole process behind the do while loop ive learnt that it loops at least once before the condition is applied but not in detail. if its not too much for you could you explain the step by step process of this loop? @Karthi – cheong kim Sep 19 '16 at 07:14
  • Hey just compare both codes, in your code you need to define the value of age two times. One above the while loop and another within the while loop, to reduce this complexity I used do while loop. In my code age value is defined only one time in do while loop, which will iterate until 0 is pressed. – Karthikeyan KR Sep 19 '16 at 07:23