如何在整数数组中使用 Math.min 和 Math.max

您可以在此处查看运算符优先顺序。


当您执行程序时,它将从左到右开始运行。调用时 m1(),您分配a=2并返回此值。所以在 a = 2 之后你的等式如下:


9 + 3 + 3 * 2 + 2 * 2 + 1 + 2

9 + 3 + 6 + 4 + 1 + 2

25


大话西游666
浏览 233回答 3
3回答

宝慕林4294392

这些函数只需要两个参数。如果你想要数组的最小值,你可以使用 IntStream。int[] a = { 1, 5, 6 };int max = IntStream.of(a).max().orElse(Integer.MIN_VALUE);int min = IntStream.of(a).min().orElse(Integer.MAX_VALUE);

慕姐8265434

你可以简单地使用,构建JavaCollection和Arrays理清这个问题。您只需要导入它们并使用它。请检查以下代码。import java.util.Arrays;import java.util.Collections;public class getMinNMax {    public static void main(String[] args) {        Integer[] num = { 2, 11, 55, 99 };        int min = Collections.min(Arrays.asList(num));        int max = Collections.max(Arrays.asList(num));        System.out.println("Minimum number of array is : " + min);        System.out.println("Maximum number of array is : " + max);    }}

守候你守候我

你可以简单地使用,构建JavaCollection和Arrays理清这个问题。您只需要导入它们并使用它。请检查以下代码。import java.util.Arrays;import java.util.Collections;public class getMinNMax {    public static void main(String[] args) {        Integer[] num = { 2, 11, 55, 99 };        int min = Collections.min(Arrays.asList(num));        int max = Collections.max(Arrays.asList(num));        System.out.println("Minimum number of array is : " + min);        System.out.println("Maximum number of array is : " + max);    }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java