简体   繁体   English

我如何创建一个带有数组的扫描仪,并在 java 中找到输入用户的最小值、最大值和平均值?

[英]How can I create a scanner with array and also find min ,max and average of input user in java?

How can I create a Scanner with array and also find min,max and average of user input in java?如何创建一个带有数组的Scanner ,并在 java 中找到用户输入的最小值、最大值和平均值?
Please give me a result or java code请给我一个结果或java代码

Code:代码:

import java.util.Scanner;

public class Array {
    public static void main(String[] args) {
        int sum = 0;
        int i = 0;
        Scanner s = new Scanner(System.in);
        int[] name = new int[5];
        while (i <= 4) {
            System.out.print("Array Number " + i + ": = ");
            name[i] = s.nextInt();
            ++i;
        }
    }
}

In order to calculate the average, you need to get the total of all the numbers and divide that by the size of your array – which you set at 5 (five).为了计算平均值,您需要获得所有数字的总和并将其除以数组的大小——您将其设置为 5(五)。

You need to keep track of the minimum and maximum.您需要跟踪最小值和最大值。 For every value entered, you need to check whether it is lower than the current minimum.对于输入的每个值,您需要检查它是否低于当前最小值。 Similarly you need to check if the entered value is greater than the current maximum.同样,您需要检查输入的值是否大于当前最大值。 You should initialize the minimum value to a very large number so that the first value entered will be lower allowing you to continue to track the minimum.您应该将最小值初始化为一个非常大的数字,以便输入的第一个值会更低,以便您继续跟踪最小值。 Likewise, initialize the maximum to a very low number so that the first value entered will be larger.同样,将最大值初始化为一个非常小的数字,以便输入的第一个值会更大。

Here is the code:这是代码:

import java.util.Scanner;

public class Array {
    public static void main(String[] args) {
        int sum = 0;
        int i = 0;
        Scanner s = new Scanner(System.in);
        int[] name = new int[5];
        int total = 0;
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;
        while (i <= 4) {
            System.out.print("Array Number " + i + ": = ");
            name[i] = s.nextInt();
            total += name[i];
            if (name[i] > max) {
                max = name[i];
            }
            if (name[i] < min) {
                min = name[i];
            }
            ++i;
        }
        System.out.println("min = " + min);
        System.out.println("max = " + max);
        double average = (double) total / 5;
        System.out.println("avg = " + average);
    }
}

I use a cast to double in the calculation of the average to ensure a double result.我在计算平均值时使用 cast to double以确保double结果。 Without the cast, the result will be an int .如果没有转换,结果将是一个int

Here is output from a sample run of the above code:这是上面代码的示例运行中的 output:

Array Number 0: = 121
Array Number 1: = 49
Array Number 2: = 81
Array Number 3: = 4
Array Number 4: = 144
min = 4
max = 144
avg = 79.8

you need to do calculations on int array afterwards之后你需要对 int 数组进行计算

Integer min = Arrays.stream(num).min(Integer::compare).get();
Integer max = Arrays.stream(num).max(Integer::compare).get();
Integer average Arrays.stream(num).average().getAsDouble();

If your talking about min,max,average you can do this如果你谈论的是最小值、最大值、平均值,你可以这样做

int sum = 0;
    int i = 0;
    Scanner s = new Scanner(System.in);
    int[] name = new int[5];
    while (i <= 4) {
        
        System.out.print("Array Number " + i + ": = ");
        name[i] = s.nextInt();
        sum = sum +name[i];
        ++i;
        
    }
    
    Arrays.sort(name);
    System.out.println("Minimum = " + name[0]);
    System.out.println("Maximum = " + name[name.length-1]);
    System.out.println("Average = " + sum/5);

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

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