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

时间:2023-03-10 06:25:39
Java软件工程的弹幕调试原则

日期:2019.4.25

博客期:061

星期四

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

如下图:

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

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

 package pvp;

 import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Random;
import java.util.Scanner; public class TestPlay6 {
public static void run(){
File f = new File("data/data.txt");
if(!f.exists())
{
System.out.println("文件不存在!");
System.exit(0);
}
//存储导入内容
String str;
//内容
Scanner sc = null;
try {
sc = new Scanner(f);
} catch (FileNotFoundException e1) {
System.out.println("文件不存在!");
System.exit(0);
}
//最大值
BigInteger rmax = new BigInteger("0");
//正数总值
BigInteger Tnum = new BigInteger("0");
//负数总值
BigInteger Fnum = new BigInteger("0");
//记录是否发生转变
int sis = 0;
//标记是第几程度
int attitude = 0; BigInteger start = BigInteger.ZERO,end = BigInteger.ZERO;
BigInteger true_start = BigInteger.ZERO,true_end = BigInteger.ZERO;
//循环
try
{
BigInteger sis_s = rmax;
BigInteger seat = new BigInteger("0");
if(!sc.hasNext())
{
System.out.println("文件内容为空!");
System.exit(0);
}
while(sc.hasNext())
{
str = sc.next();
BigInteger p = new BigInteger(str);
seat = seat.add(BigInteger.ONE);
end = seat;
if(attitude==0) //---------------------------------------[寻找第一个正数]
{
if(p.compareTo(new BigInteger("0"))<=0)
;
else
{
start = seat;
Tnum = p;
attitude = 1;
}
}
else if(attitude==1) //---------------------------------------[上一个数为正数]
{
if(p.compareTo(new BigInteger("0"))<0)
{
if(sis==0)
{
sis = 1;
Fnum.add(p);
}
else
{
Fnum = p;
}
attitude = -1;
}
else
{
Tnum = Tnum.add(p);
} if(Tnum.compareTo(rmax)>0)
{
rmax = Tnum;
true_start = start;
true_end = end;
}
}
else //---------------------------------------[上一个数为负数]
{
if(p.compareTo(new BigInteger("0"))>0)
{
attitude = 1;
if(Tnum.compareTo(new BigInteger("0").subtract(Fnum))>0)
{
Tnum = Tnum.add(Fnum).add(p);
}
else
{
Tnum = p;
start = seat;
}
}
else
{
Fnum = Fnum.add(p);
}
}
if(p.compareTo(rmax)>0)
{
rmax = p;
true_start = start;
true_end = end;
}
if(p.compareTo(Tnum)>0)
{
rmax = Tnum;
true_start = start;
true_end = end;
}
if(rmax.equals(sis_s))
System.out.println("数据编号:"+seat.toString()+"\t,新数大小:"+p.toString()+"\t,正数总值:"+Tnum.toString()+"\t,负数总值:"+Fnum.toString()+"\t,数组从第"+true_start.toString()+"\t位开始到第"+true_end.toString()+"\t位结束,最大值:"+rmax.toString()+"\t");
else
{
System.err.println("数据编号:"+seat.toString()+"\t,新数大小:"+p.toString()+"\t,正数总值:"+Tnum.toString()+"\t,负数总值:"+Fnum.toString()+"\t,数组从第"+true_start.toString()+"\t位开始到第"+true_end.toString()+"\t位结束,最大值:"+rmax.toString()+"\t");
sis_s = rmax;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
} }
catch( NumberFormatException e){
System.out.println("输入内容不是数字!");
System.exit(0);
}
sc.close();
}
public static void run2(int ls){
File f = new File("data/data.txt");
if(!f.exists())
{
System.out.println("文件不存在!");
System.exit(0);
}
//存储导入内容
String str;
//内容
Scanner sc = null;
try {
sc = new Scanner(f);
} catch (FileNotFoundException e1) {
System.out.println("文件不存在!");
System.exit(0);
}
//最大值
BigInteger rmax = new BigInteger("0");
//正数总值
BigInteger Tnum = new BigInteger("0");
//负数总值
BigInteger Fnum = new BigInteger("0");
//记录是否发生转变
int sis = 0;
//标记是第几程度
int attitude = 0; BigInteger start = BigInteger.ZERO,end = BigInteger.ZERO;
BigInteger true_start = BigInteger.ZERO,true_end = BigInteger.ZERO;
//循环
try
{
BigInteger sis_s = rmax;
BigInteger seat = new BigInteger("0");
if(!sc.hasNext())
{
System.out.println("文件内容为空!");
System.exit(0);
}
BigInteger sss = new BigInteger(Integer.toString(ls));
while(sc.hasNext())
{
str = sc.next();
BigInteger p = new BigInteger(str);
seat = seat.add(BigInteger.ONE);
end = seat;
if(attitude==0) //---------------------------------------[寻找第一个正数]
{
if(p.compareTo(new BigInteger("0"))<=0)
;
else
{
start = seat;
Tnum = p;
attitude = 1;
}
}
else if(attitude==1) //---------------------------------------[上一个数为正数]
{
if(p.compareTo(new BigInteger("0"))<0)
{
if(sis==0)
{
sis = 1;
Fnum.add(p);
}
else
{
Fnum = p;
}
attitude = -1;
}
else
{
Tnum = Tnum.add(p);
} if(Tnum.compareTo(rmax)>0)
{
rmax = Tnum;
true_start = start;
true_end = end;
}
}
else //---------------------------------------[上一个数为负数]
{
if(p.compareTo(new BigInteger("0"))>0)
{
attitude = 1;
if(Tnum.compareTo(new BigInteger("0").subtract(Fnum))>0)
{
Tnum = Tnum.add(Fnum).add(p);
}
else
{
Tnum = p;
start = seat;
}
}
else
{
Fnum = Fnum.add(p);
}
}
if(p.compareTo(rmax)>0)
{
rmax = p;
true_start = start;
true_end = end;
}
if(p.compareTo(Tnum)>0)
{
rmax = Tnum;
true_start = start;
true_end = end;
}
if(sss.compareTo(seat)<=0)
{
if(rmax.equals(sis_s))
System.out.println("数据编号:"+seat.toString()+"\t,新数大小:"+p.toString()+"\t,正数总值:"+Tnum.toString()+"\t,负数总值:"+Fnum.toString()+"\t,数组从第"+true_start.toString()+"\t位开始到第"+true_end.toString()+"\t位结束,最大值:"+rmax.toString()+"\t");
else
{
System.err.println("数据编号:"+seat.toString()+"\t,新数大小:"+p.toString()+"\t,正数总值:"+Tnum.toString()+"\t,负数总值:"+Fnum.toString()+"\t,数组从第"+true_start.toString()+"\t位开始到第"+true_end.toString()+"\t位结束,最大值:"+rmax.toString()+"\t");
sis_s = rmax;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
} }
catch( NumberFormatException e){
System.out.println("输入内容不是数字!");
System.exit(0);
}
sc.close();
}
public static void FileMaker(BigInteger leng,BigInteger min,BigInteger max){
File f = new File("data/data.txt");
if(!f.exists())
{
try {
f.createNewFile();
} catch (IOException e) {
//Do Nothing...
}
}
PrintWriter pw = null;
try {
pw = new PrintWriter(f);
} catch (FileNotFoundException e) {
System.out.println("文件不存在!");
System.exit(0);
}
Random ra = new Random(System.currentTimeMillis());
BigInteger bi = new BigInteger("1"); BigInteger sumL = max.subtract(min).add(BigInteger.ONE); while(bi.compareTo(leng)<=0)
{
long x = ra.nextLong();
BigInteger bnum = new BigInteger(Long.toString(x));
bnum = bnum.mod(sumL).add(min);
pw.println(bnum.toString());
bi = bi.add(BigInteger.ONE);
}
pw.close();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入数据量:");
String str = sc.next();
System.out.println("请输入数据范围:");
String str1 = sc.next();
String str2 = sc.next();
System.out.println("确定是否查看关键步骤");
boolean isKey = false;
try{
isKey = sc.nextBoolean();
BigInteger leng = new BigInteger(str);
BigInteger num1 = new BigInteger(str1);
BigInteger num2 = new BigInteger(str2);
if(num1.compareTo(num2)==0)
throw new Exception();
else if(leng.compareTo(BigInteger.ZERO)<0)
throw new Exception();
else
{
if(num1.compareTo(num2)<0)
FileMaker(leng,num1,num2);
else
FileMaker(leng,num2,num1);
}
}catch(Exception e){
System.out.println("爷爷!出现问题了!");
System.exit(0);
} if(!isKey)
run();
else
{
run();
try{
int l = sc.nextInt();
run2(l);
}catch (Exception e){
//Do Nothing...
}
}
sc.close();
}
}

TestPlay6