增添的新要求:
1、按 单步执行 键, 在 GUI 看到你的程序是如何一步一步算出目前最大子数组的范围,当前计算到的临时子数组是在哪里。
2、按 自动运行 键, 在 GUI 看到程序自动运行,并自动显示过程, 每次改变状态的时候要稍作停留 (例如 1 秒钟的时间)。
3、最好有一个 倒带 / 回滚 的键, 让用户可以看清楚关键的几步。
(其他基于上一篇博客中的循环数组)
我实现的比较粗略,有待改进。
package about_bank_account; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.math.BigInteger; import java.util.Scanner; public class Test { @SuppressWarnings("resource") public static void main (String[] args) throws IOException, InterruptedException{ int bushu = 1;//记录运算步骤 String line = null; String snum[] = new String[100]; String sp[] = null; File file = new File("Number.txt"); BufferedReader br = new BufferedReader(new FileReader(file)); int temp = 0; int k = 0; int i = 0; BigInteger big[] = new BigInteger[100]; BigInteger bigrmax = new BigInteger("0"); BigInteger bigmax = new BigInteger("0"); int jianceshu = 0; String jilu[] = new String[100]; //读文件 while((line=br.readLine())!=null) { sp = line.split(" ");//按空格进行分割 for(i=0;i<sp.length;i++){ snum[temp] = sp[i]; temp++; //System.out.println("第"+ bushu++ +"步:"+"读入文件中的数:"+sp[i]);//************************ } } //检测文件中异常 try { for(i = 0; i<snum.length; i++) {//转化为大数 if(snum[i] != null) { big[i] = new BigInteger(snum[i]); //System.out.println("第"+ bushu++ +"步:"+"转化为大数:"+big[i]);//************************ } } } catch(NumberFormatException e) { System.out.println("文件中存在异常字符!"); jianceshu = 1; } //若无异常,执行 if(jianceshu == 0) { i = 0; while(big[i] != null) { k++; i++; //System.out.println("第"+ bushu++ +"步:"+"计算数组中元素的个数:"+"当前为"+k);//************************ } //向后添加 int ii = 0; int kk = k+k-1; for(i = k; i<kk; i++) { big[i] = big[ii]; ii++; //System.out.println("第"+ bushu++ +"步:"+"在数组最后重复添加一遍元素-----"+"当前添加的数"+big[i]);//************************ } //求子数组最大值 BigInteger b0=new BigInteger("0"); bigrmax = big[0]; int geshu = 0; int zishuzushu = 1; int xiabiao1 = 0; int xiabiao2; int jiluxiabiao = 0; for(i = 0; i<kk; i++) { if((bigmax.compareTo(b0) < 0) || bigmax.equals(b0)){ bigmax = big[i]; xiabiao1 = i; //System.out.println("第"+ bushu++ +"步:"+"重新加和");//************************ }else { bigmax = bigmax.add(big[i]); geshu++; xiabiao2 = i; if(xiabiao2 >= k) { xiabiao2 = xiabiao2 - k; } Thread.sleep(1000); if(geshu <= k) { System.out.println("第"+ bushu++ +"步:"+"当前子数组元素和:"+bigmax+";当前子数组元素个数:"+geshu+";已检查"+ zishuzushu++ +"个子数组"+";子数组和的下标为"+xiabiao1+"到"+xiabiao2);//************************ jilu[jiluxiabiao++] = "第"+ bushu +"步:"+"当前子数组元素和:"+bigmax+";当前子数组元素个数:"+geshu+";已检查"+ zishuzushu++ +"个子数组"+";子数组和的下标为"+xiabiao1+"到"+xiabiao2; } } if((bigrmax.compareTo(bigmax) < 0)&&(geshu <= k)) { bigrmax = bigmax; //System.out.println("第"+ bushu++ +"步:"+"当前最大子数组元素和:"+bigrmax);//************************ } } System.out.println("结果:"+bigrmax); } int select = 0; int tbuzhou = -1; Scanner sc = new Scanner(System.in); while(select != 2) { System.out.print("如需回滚请输入1,如需退出请输入2:"); select = sc.nextInt(); if(select == 1) { System.out.print("请输入需回滚到的步骤(共"+ --bushu +"步):"); tbuzhou = sc.nextInt(); tbuzhou--; System.out.println(jilu[tbuzhou]); } } } }