不知道怎么了,今天坐在电脑前面就想写写代码了。废话不多说。
正如题目所说:输入一堆数值,求平均值,最大值,最小值,和
代码直接粘上:
Scanner input = new Scanner(System.in); String [] str = input.nextLine().split(" "); int [] count = new int[str.length]; for (int i = 0; i < str.length; i++) { count[i] = Integer.parseInt(str[i]); } //求最小值 int min = 0,max = 0,sum = count[0]; if(count[0] > count[1]){ max = count[0]; min = count[1]; }else{ max = count[1]; min = count[0]; } for (int i = 1; i < count.length; i++) { if(min > count[i]){ min = count[i]; } if(max < count[i]){ max = count[i]; } sum += count[i]; } System.out.println("最小值是:" + min); System.out.println("最大值是:" + max); System.out.println("和是:" + sum); System.out.println("平均值是:" + sum * 1.0 / count.length );
ok,就是这样了。