1 package geren06; 2 import java.io.*; 3 import java.util.Scanner; 4 import java.util.ArrayList; 5 import java.util.InputMismatchException; 6 public class xianshi 7 { 8 9 public static void main(String[] args) 10 { 11 long start = System.currentTimeMillis(); 12 Scanner input=new Scanner(System.in); 13 int flag=0; 14 int flag2=0; 15 16 17 int LENGTH=20;//数量 18 int FANWEI=10;// n : -n ~ +n 19 int inum; 20 int j=0; 21 int m=0; 22 int i=0,r=0,k=0; 23 int n=0; 24 String snum=""; 25 String fileName="number.txt"; 26 int [] num=new int[LENGTH]; 27 int array[]=new int[LENGTH]; 28 long maxsum=array[0]; 29 long maxstart=array[0]; 30 31 try 32 { 33 File f=new File(fileName); 34 if(!f.exists()) 35 { 36 throw new Exception("文件"+fileName+"不存在"); 37 } 38 39 //写入 40 FileWriter fw=new FileWriter(f); 41 PrintWriter pw=new PrintWriter(fw); 42 //for(r=0;r<10;r++) 43 //{ 44 for( k=0;k<LENGTH;k++,m++) 45 { 46 inum=(int)(1+Math.random()*FANWEI*2)-FANWEI; 47 pw.print(inum+" "); 48 if(m==20) 49 { 50 pw.print(" "); 51 m=0; 52 } 53 } 54 55 //} 56 pw.flush(); 57 58 59 //读取 60 do 61 { 62 flag=0; 63 j=0; 64 m=0; 65 maxsum=array[0]; 66 maxstart=array[0]; 67 System.out.println("1 自动 2 单步(回车) 输入d倒带"); 68 flag=input.nextInt(); 69 input.nextLine(); 70 if(flag2==1) 71 { 72 flag=1; 73 } 74 FileReader fr=new FileReader(f); 75 Scanner in=new Scanner(fr); 76 if(!in.hasNext()) 77 { 78 throw new Exception("文件"+fileName+"内容为空"); 79 } 80 81 while(in.hasNext()) 82 { 83 84 if(j>=LENGTH) 85 86 { 87 System.out.println("数字过多,多于"+LENGTH+"将不予读取"); 88 break; 89 } 90 if(maxstart<0) 91 { 92 if(flag2==0) 93 { 94 System.out.print(" "+m+"会使和变小 maxstart: 0"); 95 } 96 maxstart=0; 97 } 98 m=in.nextInt(); 99 maxstart+=m; 100 if(flag==1&&flag2==0) 101 { 102 Thread.sleep(1000); 103 104 System.out.println(); 105 } 106 if(flag==2) 107 { 108 109 if(input.nextLine().equals("d")) 110 { 111 System.out.println("请输入序号:"); 112 n=input.nextInt(); 113 flag2=1; 114 break; 115 116 } 117 } 118 if(flag2==0) 119 { 120 System.out.print("# j: "+j+" "); 121 System.out.print("m: "+m+" "); 122 System.out.print("maxstart: "+maxstart+" "); 123 System.out.print("maxsum: "+maxsum); 124 } 125 126 127 if(maxstart>=(Long.MAX_VALUE/2-1)||maxstart<=(Long.MIN_VALUE/2-1)) 128 { 129 throw new Exception("数字过大溢出"); 130 } 131 if(maxstart>maxsum) 132 { 133 if(flag2==0)System.out.print(" "+maxstart+" > "+maxsum+" --交换-- "+"maxsum: "+maxsum); 134 maxsum=maxstart; 135 } 136 137 j++; 138 if(j==n) 139 { 140 flag2=0; 141 } 142 } 143 if(flag2==1) 144 { 145 continue; 146 } 147 148 149 System.out.println("答案为: "+maxsum); 150 System.out.println("是否继续?输入0结束"); 151 }while(flag!=0); 152 153 154 } 155 catch(InputMismatchException e2) 156 { 157 System.out.println("数字过大或输入格式不对"); 158 } 159 catch(Exception e) 160 { 161 System.out.println("错误于: "+i); 162 System.out.println("错误!!!"); 163 System.out.println(e.getMessage()); 164 e.printStackTrace(); 165 } 166 167 168 long end = System.currentTimeMillis(); 169 //System.out.println("程序运行时间:"+(end-start)+"ms"); 170 171 172 173 174 175 176 177 } 178 }
设立flag,合理使用if else。