Java软件工程的弹幕调试原则

日期:2019.4.25

博客期:061

星期四

       今天是把很久之前的那个相关程序——一维数组的最大和的子数组的求取信息,我们今天的任务就是把每一步的信息都要进行输出查看!

       如下图:

  

  1 package pvp;
  2 
  3 import java.io.File;
  4 import java.io.FileNotFoundException;
  5 import java.io.IOException;
  6 import java.io.PrintWriter;
  7 import java.math.BigInteger;
  8 import java.util.Random;
  9 import java.util.Scanner;
 10 
 11 public class TestPlay6 {
 12     public static void run(){
 13         File f = new File("data/data.txt");
 14         if(!f.exists())
 15         {
 16             System.out.println("文件不存在!");
 17             System.exit(0);
 18         }
 19         //存储导入内容
 20         String str;
 21         //内容
 22         Scanner sc = null;
 23         try {
 24             sc = new Scanner(f);
 25         } catch (FileNotFoundException e1) {
 26             System.out.println("文件不存在!");
 27             System.exit(0);
 28         }
 29         //最大值
 30         BigInteger rmax = new BigInteger("0");
 31         //正数总值
 32         BigInteger Tnum = new BigInteger("0");
 33         //负数总值
 34         BigInteger Fnum = new BigInteger("0");
 35         //记录是否发生转变
 36         int sis = 0;
 37         //标记是第几程度
 38         int attitude = 0;
 39         
 40         BigInteger start = BigInteger.ZERO,end = BigInteger.ZERO;
 41         BigInteger true_start = BigInteger.ZERO,true_end = BigInteger.ZERO;
 42         //循环
 43         try
 44         {
 45             BigInteger sis_s = rmax;
 46             BigInteger seat = new BigInteger("0");
 47             if(!sc.hasNext())
 48             {
 49                 System.out.println("文件内容为空!");
 50                 System.exit(0);
 51             }
 52             while(sc.hasNext())
 53             {
 54                 str = sc.next();
 55                 BigInteger p = new BigInteger(str);
 56                 seat = seat.add(BigInteger.ONE);
 57                 end = seat;
 58                 if(attitude==0)                    //---------------------------------------[寻找第一个正数]
 59                 {
 60                     if(p.compareTo(new BigInteger("0"))<=0)
 61                         ;
 62                     else
 63                     {
 64                         start = seat;
 65                         Tnum = p;
 66                         attitude = 1;
 67                     }
 68                 }
 69                 else if(attitude==1)            //---------------------------------------[上一个数为正数]
 70                 {
 71                     if(p.compareTo(new BigInteger("0"))<0)
 72                     {
 73                         if(sis==0)
 74                         {
 75                             sis = 1;
 76                             Fnum.add(p);
 77                         }
 78                         else
 79                         {
 80                             Fnum = p;
 81                         }
 82                         attitude = -1;
 83                     }
 84                     else
 85                     {
 86                         Tnum = Tnum.add(p);
 87                     }
 88 
 89                     if(Tnum.compareTo(rmax)>0)
 90                     {
 91                         rmax = Tnum;
 92                         true_start = start;
 93                         true_end = end;
 94                     }
 95                 }
 96                 else                            //---------------------------------------[上一个数为负数]
 97                 {
 98                     if(p.compareTo(new BigInteger("0"))>0)
 99                     {
100                         attitude = 1;
101                         if(Tnum.compareTo(new BigInteger("0").subtract(Fnum))>0)
102                         {
103                             Tnum = Tnum.add(Fnum).add(p);
104                         }
105                         else
106                         {
107                             Tnum = p;
108                             start = seat;
109                         }
110                     }
111                     else
112                     {
113                         Fnum = Fnum.add(p);
114                     }
115                 }
116                 if(p.compareTo(rmax)>0)
117                 {
118                     rmax = p;
119                     true_start = start;
120                     true_end = end;
121                 }
122                 if(p.compareTo(Tnum)>0)
123                 {
124                     rmax = Tnum;
125                     true_start = start;
126                     true_end = end;
127                 }
128                 if(rmax.equals(sis_s))
129                     System.out.println("数据编号:"+seat.toString()+"	,新数大小:"+p.toString()+"	,正数总值:"+Tnum.toString()+"	,负数总值:"+Fnum.toString()+"	,数组从第"+true_start.toString()+"	位开始到第"+true_end.toString()+"	位结束,最大值:"+rmax.toString()+"	");
130                 else
131                 {
132                     System.err.println("数据编号:"+seat.toString()+"	,新数大小:"+p.toString()+"	,正数总值:"+Tnum.toString()+"	,负数总值:"+Fnum.toString()+"	,数组从第"+true_start.toString()+"	位开始到第"+true_end.toString()+"	位结束,最大值:"+rmax.toString()+"	");
133                     sis_s = rmax;
134                 }
135                 try {
136                     Thread.sleep(1000);
137                 } catch (InterruptedException e) {
138                     // TODO 自动生成的 catch 块
139                     e.printStackTrace();
140                 }
141             }
142             
143         }
144         catch( NumberFormatException e){
145             System.out.println("输入内容不是数字!");
146             System.exit(0);
147         }
148         sc.close();
149     }
150     public static void run2(int ls){
151         File f = new File("data/data.txt");
152         if(!f.exists())
153         {
154             System.out.println("文件不存在!");
155             System.exit(0);
156         }
157         //存储导入内容
158         String str;
159         //内容
160         Scanner sc = null;
161         try {
162             sc = new Scanner(f);
163         } catch (FileNotFoundException e1) {
164             System.out.println("文件不存在!");
165             System.exit(0);
166         }
167         //最大值
168         BigInteger rmax = new BigInteger("0");
169         //正数总值
170         BigInteger Tnum = new BigInteger("0");
171         //负数总值
172         BigInteger Fnum = new BigInteger("0");
173         //记录是否发生转变
174         int sis = 0;
175         //标记是第几程度
176         int attitude = 0;
177         
178         BigInteger start = BigInteger.ZERO,end = BigInteger.ZERO;
179         BigInteger true_start = BigInteger.ZERO,true_end = BigInteger.ZERO;
180         //循环
181         try
182         {
183             BigInteger sis_s = rmax;
184             BigInteger seat = new BigInteger("0");
185             if(!sc.hasNext())
186             {
187                 System.out.println("文件内容为空!");
188                 System.exit(0);
189             }
190             BigInteger sss = new BigInteger(Integer.toString(ls));
191             while(sc.hasNext())
192             {
193                 str = sc.next();
194                 BigInteger p = new BigInteger(str);
195                 seat = seat.add(BigInteger.ONE);
196                 end = seat;
197                 if(attitude==0)                    //---------------------------------------[寻找第一个正数]
198                 {
199                     if(p.compareTo(new BigInteger("0"))<=0)
200                         ;
201                     else
202                     {
203                         start = seat;
204                         Tnum = p;
205                         attitude = 1;
206                     }
207                 }
208                 else if(attitude==1)            //---------------------------------------[上一个数为正数]
209                 {
210                     if(p.compareTo(new BigInteger("0"))<0)
211                     {
212                         if(sis==0)
213                         {
214                             sis = 1;
215                             Fnum.add(p);
216                         }
217                         else
218                         {
219                             Fnum = p;
220                         }
221                         attitude = -1;
222                     }
223                     else
224                     {
225                         Tnum = Tnum.add(p);
226                     }
227 
228                     if(Tnum.compareTo(rmax)>0)
229                     {
230                         rmax = Tnum;
231                         true_start = start;
232                         true_end = end;
233                     }
234                 }
235                 else                            //---------------------------------------[上一个数为负数]
236                 {
237                     if(p.compareTo(new BigInteger("0"))>0)
238                     {
239                         attitude = 1;
240                         if(Tnum.compareTo(new BigInteger("0").subtract(Fnum))>0)
241                         {
242                             Tnum = Tnum.add(Fnum).add(p);
243                         }
244                         else
245                         {
246                             Tnum = p;
247                             start = seat;
248                         }
249                     }
250                     else
251                     {
252                         Fnum = Fnum.add(p);
253                     }
254                 }
255                 if(p.compareTo(rmax)>0)
256                 {
257                     rmax = p;
258                     true_start = start;
259                     true_end = end;
260                 }
261                 if(p.compareTo(Tnum)>0)
262                 {
263                     rmax = Tnum;
264                     true_start = start;
265                     true_end = end;
266                 }
267                 if(sss.compareTo(seat)<=0)
268                 {
269                     if(rmax.equals(sis_s))
270                         System.out.println("数据编号:"+seat.toString()+"	,新数大小:"+p.toString()+"	,正数总值:"+Tnum.toString()+"	,负数总值:"+Fnum.toString()+"	,数组从第"+true_start.toString()+"	位开始到第"+true_end.toString()+"	位结束,最大值:"+rmax.toString()+"	");
271                     else
272                     {
273                         System.err.println("数据编号:"+seat.toString()+"	,新数大小:"+p.toString()+"	,正数总值:"+Tnum.toString()+"	,负数总值:"+Fnum.toString()+"	,数组从第"+true_start.toString()+"	位开始到第"+true_end.toString()+"	位结束,最大值:"+rmax.toString()+"	");
274                         sis_s = rmax;
275                     }
276                     try {
277                         Thread.sleep(1000);
278                     } catch (InterruptedException e) {
279                         // TODO 自动生成的 catch 块
280                         e.printStackTrace();
281                     }
282                 }
283             }
284             
285         }
286         catch( NumberFormatException e){
287             System.out.println("输入内容不是数字!");
288             System.exit(0);
289         }
290         sc.close();
291     }
292     public static void FileMaker(BigInteger leng,BigInteger min,BigInteger max){
293         File f = new File("data/data.txt");
294         if(!f.exists())
295         {
296             try {
297                 f.createNewFile();
298             } catch (IOException e) {
299                 //Do Nothing...
300             }
301         }
302         PrintWriter pw = null;
303         try {
304             pw = new PrintWriter(f);
305         } catch (FileNotFoundException e) {
306             System.out.println("文件不存在!");
307             System.exit(0);
308         }
309         Random ra = new Random(System.currentTimeMillis());
310         BigInteger bi = new BigInteger("1");
311         
312         BigInteger sumL = max.subtract(min).add(BigInteger.ONE);
313         
314         while(bi.compareTo(leng)<=0)
315         {
316             long x = ra.nextLong();
317             BigInteger bnum = new BigInteger(Long.toString(x));
318             bnum = bnum.mod(sumL).add(min);
319             pw.println(bnum.toString());
320             bi = bi.add(BigInteger.ONE);
321         }
322         pw.close();
323     }
324     public static void main(String[] args) {
325         Scanner sc = new Scanner(System.in);
326         System.out.println("请输入数据量:");
327         String str = sc.next();
328         System.out.println("请输入数据范围:");
329         String str1 = sc.next();
330         String str2 = sc.next();
331         System.out.println("确定是否查看关键步骤");
332         boolean isKey = false;
333         try{
334             isKey = sc.nextBoolean();
335             BigInteger leng = new BigInteger(str);
336             BigInteger num1 = new BigInteger(str1);
337             BigInteger num2 = new BigInteger(str2);
338             if(num1.compareTo(num2)==0)
339                 throw new Exception();
340             else if(leng.compareTo(BigInteger.ZERO)<0)
341                 throw new Exception();
342             else
343             {
344                 if(num1.compareTo(num2)<0)
345                     FileMaker(leng,num1,num2);
346                 else
347                     FileMaker(leng,num2,num1);
348             }
349         }catch(Exception e){
350             System.out.println("爷爷!出现问题了!");
351             System.exit(0);
352         }
353         
354         if(!isKey)
355             run();
356         else
357         {
358             run();
359             try{
360                 int l = sc.nextInt();
361                 run2(l);
362             }catch (Exception e){
363                 //Do Nothing...
364             }
365         }
366         sc.close();
367     }
368 }
TestPlay6
原文地址:https://www.cnblogs.com/onepersonwholive/p/10770522.html