按 自动运行 键, 在 GUI 看到程序自动运行,并自动显示过程, 每次改变状态的时候要稍作停留 (例如 1 秒钟的时间)
最好有一个 倒带 / 回滚 的键, 让用户可以看清楚关键的几步。
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.Scanner; public class Shuzu extends Thread{ public Shuzu(String name) { // 参数为线程名称 setName(name); } public void run() { // 覆盖run()方法 try { chansheng(); read(); String[] rang=s.split("[^(-9--10-9)]"); int sum[]=new int[rang.length]; int sum1=0; int sum2=0; int sum3=0; int a=0; int b=0; for(int i=0;i<rang.length;i++) { sum[i]=Integer.parseInt(rang[i]); } sum1=sum[0]; for(int i=0;i<rang.length;i++) { sum2=sum[i]; for(int j=i+1;j<rang.length;j++) { sum2=sum2+sum[j]; if(sum2>sum1) { sum1=sum2; a=i; b=j; } sum3=sum3+1; System.out.println("第"+sum3+"步"+" "+"目前的最大子数组和为:"+sum1+" "+"数组元素为:第"+a+"个到第"+b+"个"); Thread.sleep(1000); } } System.out.println("最大和为:"+sum1); System.out.println("请输入要查询哪一步:"); Scanner sc1=new Scanner(System.in); int c=sc1.nextInt(); int d=0; if(c>sum3) { System.out.println("请正确输入!"); }else { while(d<=c){ for(int i=0;i<rang.length;i++) { sum2=sum[i]; for(int j=i+1;j<rang.length;j++) { sum2=sum2+sum[j]; if(sum2>sum1) { sum1=sum2; a=i; b=j; } d=d+1; } } } System.out.println("第"+c+"步"+" "+"目前的最大子数组和为:"+sum1+" "+"数组元素为:第"+a+"个到第"+b+"个"); } } catch (Exception e) { e.printStackTrace(); } } static String s=""; public static void main(String[] args) throws IOException { Thread thread1 = new Thread(new Shuzu("线程1")); // 创建线程1 thread1.start(); } public static String read() throws IOException { FileInputStream fis =new FileInputStream("leitianyushishabi.txt"); BufferedReader br =new BufferedReader(new InputStreamReader(fis)); String temp=" "; while((temp = br.readLine())!=null) { s= s+temp +" "; } fis.close(); br.close(); return s; } public static void chansheng() throws IOException { int n; System.out.println("请输入要产生的数量:"); Scanner sc=new Scanner(System.in); n=sc.nextInt(); File file1=new File("leitianyushishabi.txt"); FileWriter out=new FileWriter(file1); BufferedWriter bw =new BufferedWriter(out); for(int i=0;i<n;i++) { int ran =(int) ((int) (Math.random()*100)-Math.random()*100); //BigInteger a=new BigInteger(String.valueOf(ran)); bw.write(String.valueOf(ran)+" "); } bw.close(); } }