关于#java#的问题

代码海岸 其他问答 1

编写简单的程序实现以下功能: (1)产生 5 000 个 1~9 999 之间的随机整数,将其存入文本文件 a.txt 中, (2)从文件中读取这 5 000 个整数,并计算其最大值,最小值和平均值。

回复

共2条回复 我来回复
  • 毕设港湾
    这个人很懒,什么都没有留下~
    评论
     public static void main(String[] args) {
            List<Integer> startArrs = new ArrayList<>();
            for (int i = 0; i < 5000; i++) {
                startArrs.add((int) (Math.random() * 9999 + 1));
            }
            BufferedWriter bw = null;
            BufferedReader br = null;
            try {
                //5000个随机数写入
                bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\a.txt")));
                for (Integer i : startArrs) {
                    bw.write(String.valueOf(i));
                    bw.newLine();
                }
                //读取随机数
                br = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\a.txt")) {
                });
                //读取到的随机数集合
                List<Integer> integerList = new ArrayList<>();
                String str = null;
                while ((str=br.readLine()) != null) {
                    integerList.add(Integer.parseInt(str));
                }
                //求最大值,最小值,平均值
                int max=0;
                int min=0;
                int avg=0;
                int sum=0;
                for (int i=0;i<integerList.size();i++){
                    if (integerList.get(i)>max){
                        max=integerList.get(i);
                    }
                    if (i==0){
                        min=integerList.get(i);
                    }
                    if (integerList.get(i)<min){
                        min=integerList.get(i);
                    }
                    sum+=integerList.get(i);
                }
                System.out.println("最大值:"+max);
                System.out.println("最小值:"+min);
                System.out.println("平均值:"+(sum/integerList.size()));
    
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bw != null) {
                        bw.close();
                    }
                    if (br != null) {
                        br.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
        }
    
    0条评论
  • 毕业设计客栈
    这个人很懒,什么都没有留下~
    评论
     public static void main(String[] args) {
            List<Integer> startArrs = new ArrayList<>();
            for (int i = 0; i < 5000; i++) {
                startArrs.add((int) (Math.random() * 9999 + 1));
            }
            BufferedWriter bw = null;
            BufferedReader br = null;
            try {
                //5000个随机数写入
                bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\a.txt")));
                for (Integer i : startArrs) {
                    bw.write(String.valueOf(i));
                    bw.newLine();
                }
                //读取随机数
                br = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\a.txt")) {
                });
                //读取到的随机数集合
                List<Integer> integerList = new ArrayList<>();
                String str = null;
                while ((str=br.readLine()) != null) {
                    integerList.add(Integer.parseInt(str));
                }
                //求最大值,最小值,平均值
                int max=0;
                int min=0;
                int avg=0;
                int sum=0;
                for (int i=0;i<integerList.size();i++){
                    if (integerList.get(i)>max){
                        max=integerList.get(i);
                    }
                    if (i==0){
                        min=integerList.get(i);
                    }
                    if (integerList.get(i)<min){
                        min=integerList.get(i);
                    }
                    sum+=integerList.get(i);
                }
                System.out.println("最大值:"+max);
                System.out.println("最小值:"+min);
                System.out.println("平均值:"+(sum/integerList.size()));
    
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bw != null) {
                        bw.close();
                    }
                    if (br != null) {
                        br.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
        }
    
    0条评论

发表回复

登录后才能评论