简体   繁体   English

计算输入数字的最大值和平均值

[英]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这就是我需要输入 1 到 120 之间的值,直到输入 0 并计算这些值的最大值和平均值,如果我编译此代码,我收到错误消息

"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但现在的问题是最大值始终为 0,平均值始终接近于 0。例如,当我输入数字时,它也会采用负值

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我想输出“每次输入无效数字时输入 1 到 120 之间的数字

ive tried changing the while condition to (age != 0 && age >=1 && age <=120) but didnt work我尝试将 while 条件更改为 (age != 0 && age >=1 && age <=120) 但没有用

It is possible for 0 to be entered immediately and average would never get set.有可能立即输入 0 并且永远不会设置平均值。 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.您可以在 print 语句之前(并在 while 循环之外)将您的调用移动到 getAve() ,因为您只需要调用它一次。

    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.年龄小于零和大于 120 是不可能的。

The value for average is not set in every logical path through your main() method, hence the warning. average的值并未在通过main()方法的每个逻辑路径中设置,因此出现警告。

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.或者,您可以利用您不需要每次通过while循环重新计算平均值的事实。 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第二个问题是你在这里使用max

maximum = getMax(maximum, age);

and here和这里

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

but your real value is maximum , so switch that too.但你的真正价值是maximum ,所以也切换一下。

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.使用 do while 循环代替 while 循环。
  • You pass max as argument for getMax() but you need to pass maximum as an argument.您将 max 作为参数传递给 getMax() 但您需要将 max 作为参数传递。
  • Double is initialized as 0. Double 被初始化为 0。

If you have any query please comment below.如果您有任何疑问,请在下面发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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