发布网友 发布时间:2022-05-25 17:16
共2个回答
热心网友 时间:2023-11-24 03:37
package test20210106;
import java.util.Arrays;
import java.util.Scanner;
public class Test01 {
static int[] arr = new int[0];
public static void main(String[] args) {
systemIn();
System.out.println("数组"+Arrays.toString(arr)+" 的各项数据如下: ");
System.out.println("总和为: "+getSum());
double d = (double)getSum()/(double)arr.length;
System.out.println("平均值: "+d);
sort();
System.out.println("最大值: "+arr[0]);
System.out.println("最小值: "+arr[arr.length-1]);
}
/**用户输入数据*/
public static void systemIn(){
System.out.println("请输入一个整数,输入0则表示输入结束,程序会执行计算程序");
Scanner sc = new Scanner(System.in);
int q = sc.nextInt();
if(q!=0){
arr = Arrays.copyOf(arr, arr.length+1);
arr[arr.length-1] = q;
systemIn();
}
}
/**求和*/
public static int getSum(){
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
/**给数组排序*/
public static void sort(){
for (int i = 0; i < arr.length; i++) {
for (int j = arr.length-1; j >i; j--) {
if(arr[j]>arr[i]){
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
}
}
热心网友 时间:2023-11-24 03:37
import java.util.Scanner;