• 个人作业续


    package 一;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Scanner;
    import java.util.Random;
    public class Maxshuzu 
    {
        public static void main(String args[])
        {
        	Random rd=new Random();
        	
            long Array[][]=new long[1000000][2];
            int count=0,length=0;
            File file = new File("shuzu.txt");
            BufferedWriter writer = null;
    		try {
    			writer = new BufferedWriter(new FileWriter(file));
    		} catch (IOException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
            for(long k=0;k<10000;k++){
            long   l   =   (long)(Math.random()*1000);
            String   s   =   ""+l;
            int   i   =   s.length();
           
    //        for(int   j=0;j <2-i;j++){
    //          s   =   "0"+s;
    //        }
            int z;
            if((z=(int) (Math.random()*100))%2==0) {
            	s="-"+s;
            }
            System.out.println(s);
            try {
    			writer.write(s);
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
            try {
    			writer.newLine();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
            }
            try {
    			writer.flush();
    		} catch (IOException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
            try {
    			writer.close();
    		} catch (IOException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
                  
     
            
            
    			BufferedReader br = null;
    			try {
    				br = new BufferedReader(new FileReader(file));
    			} catch (FileNotFoundException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}	
    				
    				String tmp;
    				try {int i=0;
    					while((tmp = br.readLine()) != null){
    						Array[i][0]=Integer.parseInt(tmp);
    						i++;
    						length++;
    					}
    				} catch (NumberFormatException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    				try {
    					br.close();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    				Array[0][1]=Array[0][0];
    		        for(int i=1;i<length;i++)
    		        {
    		            if(Array[i-1][1]<=0)
    		            {
    		                Array[i][1]=Array[i][0];
    		            }
    		            if(Array[i-1][1]>0)
    		            {
    		                Array[i][1]=Array[i-1][1]+Array[i][0];
    		            }
    		        }
    		        long Max=Array[0][1];
    		        for(int i=1;i<length;i++)
    		        {
    		            if(Array[i][1]>Max)
    		            {
    		                Max=Array[i][1];
    		            }
    		        }
    		        System.out.println("数组最大值为:"+Max);
    		    }
    
    }
    

      通过使用Scanner类随机产生数字,使用bufferedWriter写入文件shuzu.txt,使用bufferedReader读出文件内容

     

    在本文件中对数据的大小只能到达正负九位数左右,对输入内容的兼容性上有问题

  • 相关阅读:
    grid 布局
    mongoose
    Nestjs 上传文件
    Nestjs 设置静态文件,public
    Centos 为Nginx 搭建https
    react组件
    namecheap 添加二级域名
    electron+react
    遍历文件,读取.wxss文件,在头部添加一条注释
    react 中的绑定事件
  • 原文地址:https://www.cnblogs.com/NCLONG/p/10549181.html
Copyright © 2020-2023  润新知