import java.util.Scanner;
public class Test {
public static void main(String[] args) {
System.out.println("请输入若干个数,每输入一个数回车确认");
System.out.println("最后输入数字-1结束输入操作");
Scanner reader = new Scanner(System.in);
double sum = 0;
double max = Double.MIN_VALUE;
double min = Double.MAX_VALUE;
double ave = 0;
double timer = 0;
double x = reader.nextDouble();
while (x != -1) {
if (x > max) {
max = x;
}
if (x < min) {
min = x;
}
timer++;
sum = sum + x;
x = reader.nextDouble();
}
ave = sum / timer;
System.out.println("和 = " + sum);
System.out.println("最大值 = " + max);
System.out.println("最小值 = " + min);
System.out.println("平均值 = " + ave);
}
}
import java.util.*;
public class demo {
public static void main(String[] args) {
System.out.println("请输入若干个数,每输入一个数回车确认");
System.out.println("最后输入数字-1结束输入操作");
Scanner sc = new Scanner(System.in);
ArrayListlist = new ArrayList ();
int count = 0;
int i ;
while((i=sc.nextInt())!=-1){
list.add(i);
count+=i;
}
Collections.sort(list);
int max = list.get(list.size()-1);
int min = list.get(0);
double avg = count*1.0/list.size();
System.out.println("最大值"+max);
System.out.println("最小值"+min);
System.out.println("平均值"+avg);
}
}
楼上的回答也可以,我这是另一种方法,看起来更明了一点,可以供你参考,喜欢的话请采纳~
public class test(){
public void static main(String [] args){
Scanner input =new Scanner(System.in);
int i=0;
int a[] =new int[]{};
while(i!=-1){
a[i] = input.next();
i++;
}
int sum=0;
for(int i=0;i
}
int avg=sum/a,length;
System.out.println("平均值:"+avg);
Arrays.sort(a);
int b=a.length;
System.out.println("最大值:"+a[b]);
System.out.println("最小值:"+a[0]);
}
}