package testw; import java.util.Scanner; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.PrintWriter; import java.util.ArrayList; import java.util.InputMismatchException; public class test { public static void main(String[] args) { long start = System.currentTimeMillis(); int LENGTH=3;//数量 int FANWEI=10;// n : -n ~ +n int inum; int j=0; int m=0; int i=0,r=0,k=0; String snum=""; String fileName="number.txt"; int [] num=new int[LENGTH]; int array[]=new int[LENGTH]; long maxsum=array[0]; long maxstart=array[0]; try { File f=new File(fileName); if(!f.exists()) { throw new Exception("文件"+fileName+"不存在"); } //写入 FileWriter fw=new FileWriter(f); PrintWriter pw=new PrintWriter(fw); //for(r=0;r<10;r++) //{ for( k=0;k<LENGTH;k++,m++) { inum=(int)(1+Math.random()*FANWEI*2)-FANWEI; pw.print(inum+" "); if(m==20) { pw.print("\n"); m=0; } } //} pw.flush(); //读取 FileReader fr=new FileReader(f); Scanner in=new Scanner(fr); if(!in.hasNext()) { throw new Exception("文件"+fileName+"内容为空"); } while(in.hasNext()) { /*if(j>=LENGTH) { System.out.println("数字过多,多于"+LENGTH+"将不予读取"); break; }*/ if(maxstart<0) { maxstart=0; } m=in.nextInt(); System.out.println(m); maxstart+=m; if(maxstart>=(Long.MAX_VALUE/2-1)||maxstart<=(Long.MIN_VALUE/2-1)) { throw new Exception("数字过大溢出"); } if(maxstart>maxsum) maxsum=maxstart; j++; } FileReader fr2=new FileReader(f); Scanner in2=new Scanner(fr2); while(in2.hasNext()) { if(j<=1)break; if(maxstart<0) { maxstart=0; } m=in2.nextInt(); System.out.println(m); maxstart+=m; if(maxstart>=(Long.MAX_VALUE/2-1)||maxstart<=(Long.MIN_VALUE/2-1)) { throw new Exception("数字过大溢出"); } if(maxstart>maxsum) maxsum=maxstart; j--; } System.out.println("max=:"+maxsum); } catch(InputMismatchException e2) { System.out.println("数字过大或输入格式不对"); } catch(Exception e) { System.out.println("错误于:\t"+i); System.out.println("错误!!!"); System.out.println(e.getMessage()); e.printStackTrace(); } long end = System.currentTimeMillis(); //System.out.println("程序运行时间:"+(end-start)+"ms"); } }
自我抑郁又自我救赎