package com.company;
public class javaDay06_4 {
public static void main(String[] args) {
//使用数组和函数来求多个数的和
int arr1[] = new int[]{1, 2, 3, 4, 5, 6};
int sum = add(arr1);
System.out.println(sum);
int max = getMax(arr1);
System.out.println(max);
System.out.println("使用脚标判断,求最值");
int max2=getMax2(arr1);
System.out.println("max2="+max2);
int min = getMin(arr1);
System.out.println(min);
}
//该函数的功能就是将数组遍历并且将数组的元素求和
public static int add(int[] arr) {
int sum = 0;
for (int x = 0; x < arr.length; x++) {
sum = sum + arr[x];
}
return sum;
}
//获取多个整数中最大值
public static int getMax(int[] arr) {
int max = arr[0];
for (int x = 1; x < arr.length; x++) {
if (arr[x] > max) {
max = arr[x];
}
}
return max;
}
//获取多个整数中最小值
public static int getMin(int[] arr) {
int min = arr[0];
for (int x = 1; x < arr.length; x++) {
if (arr[x] < min) {
min = arr[x];
}
}
return min;
}
//获取最大值,脚标方式
public static int getMax2(int[] arr) {
int max = 0;
for (int x = 0; x < arr.length; x++) {
if (arr[x] > arr[max]) {
max=x;
}
}
return arr[max];
}
}