int max=Integer.MIN_VALUE;//求最大值
int min=Integer.MAX_VALUE;//求最小值
int sum=0;//求平均值
int count=0;
while(true){
try{
int kk=dis.readInt();
if (kk>max)
max=kk;
if (kk<min)
min=kk;
sum+=kk;//求和求平均
count++;
double res = 1. * sum / count;
3.输入输出流代码实现,写入a1.data
import java.io.*;
import java.util.concurrent.ThreadLocalRandom;
public class count {
public static void main(String[] args)throws Exception{
ThreadLocalRandom r=ThreadLocalRandom.current();//等于new出一个random
int[]arr=new int[5000];
for(int i=0; i<arr.length; i++){
arr[i]=r.nextInt( 9999)+1;//0-9999改1-10000
}
try(
DataOutputStream dos=new DataOutputStream(new BufferedOutputStream(new FileOutputStream("a1.data")))
) {
for (int tmp : arr)
dos.writeInt(tmp);
}
try (
DataInputStream dis=new DataInputStream(new BufferedInputStream(new FileInputStream("a1.data")))
) {
int max=Integer.MIN_VALUE;//求最大值
int min=Integer.MAX_VALUE;//求最小值
int sum=0;//求平均值
int count=0;
while(true){
try{
int kk=dis.readInt();
if (kk>max)
max=kk;
if (kk<min)
min=kk;
sum+=kk;//求和求平均
count++;
}catch (EOFException e){
break;
}
}
double res = 1. * sum / count;
System.out.printf("最大值为%d,最小值为%d,平均值为%f",max,min,res);
}
}
}