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

huangapple go评论72阅读模式
英文:

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

问题

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("数组元素 " + i + ": = ");
            name[i] = s.nextInt();
            ++i;
        }
    }
}
英文:

How can I create a Scanner with array and also find min ,max and average of user input in java?
Please give me a result or java code

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 &lt;= 4) {
	        System.out.print(&quot;Array Number &quot; + i + &quot;: = &quot;);
	        name[i] = s.nextInt();
	        ++i;
        }
    }
}

答案1

得分: 1

为了计算平均值,您需要获取所有数字的总和,然后将其除以数组的大小,即您设置为5(五)。

您需要跟踪最小值和最大值。对于每个输入的值,您需要检查它是否小于当前的最小值。类似地,您需要检查输入的值是否大于当前的最大值。您应该将最小值初始化为一个非常大的数字,以便第一个输入的值会更低,从而允许您继续跟踪最小值。同样,将最大值初始化为一个非常低的数字,以便第一个输入的值会更大。

以下是代码:

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);
    }
}

我在计算平均值时使用了double强制转换,以确保得到一个double结果。如果没有进行转换,结果将是一个int

以下是上述代码的示例运行输出:

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
英文:

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 &ndash; which you set at 5 (five).

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 &lt;= 4) {
            System.out.print(&quot;Array Number &quot; + i + &quot;: = &quot;);
            name[i] = s.nextInt();
            total += name[i];
            if (name[i] &gt; max) {
                max = name[i];
            }
            if (name[i] &lt; min) {
                min = name[i];
            }
            ++i;
        }
        System.out.println(&quot;min = &quot; + min);
        System.out.println(&quot;max = &quot; + max);
        double average = (double) total / 5;
        System.out.println(&quot;avg = &quot; + average);
    }
}

I use a cast to double in the calculation of the average to ensure a double result. Without the cast, the result will be an int.

Here is output from a sample run of the above code:

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

答案2

得分: 1

你需要在整数数组上进行计算操作

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

you need to do calculations on int array afterwards

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();

答案3

得分: 0

如果你在谈论最小值、最大值和平均值,你可以这样做:

int sum = 0;
int i = 0;
Scanner s = new Scanner(System.in);
int[] name = new int[5];
while (i <= 4) {
    System.out.print("数组编号 " + i + ": = ");
    name[i] = s.nextInt();
    sum = sum + name[i];
    ++i;
}

Arrays.sort(name);
System.out.println("最小值 = " + name[0]);
System.out.println("最大值 = " + name[name.length-1]);
System.out.println("平均值 = " + sum/5);
英文:

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 &lt;= 4) {
        
        System.out.print(&quot;Array Number &quot; + i + &quot;: = &quot;);
        name[i] = s.nextInt();
        sum = sum +name[i];
        ++i;
        
    }
    
    Arrays.sort(name);
    System.out.println(&quot;Minimum = &quot; + name[0]);
    System.out.println(&quot;Maximum = &quot; + name[name.length-1]);
    System.out.println(&quot;Average = &quot; + sum/5);

huangapple
  • 本文由 发表于 2020年8月19日 14:23:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63481104.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定