
package ActualCombat;import java.util.Iterator;public class test {public static void main(String[] args) {int s = 0,w = 0;int numbers[] = {1, -2, 3, 10, -4, 7, 2, -5}; //创建原数组int[] memory = new int[100]; //备忘录int[] maxnub = new int[numbers.length]; //存储最大值int[] end = new int[numbers.length];//子序列最后的位置for (int i = 0; i < numbers.length; i++) {maxnub[i] = numbers[i];memory[i] = numbers[i];for (int j = i+1; j < numbers.length; j++) {memory[j] = memory[j-1]+numbers[j];if (memory[j] > maxnub[i]) {maxnub[i] = memory[j];end[i] = j;}}for (int k = 0; k <= numbers.length; k++) {memory[k] = 0;}}for (int i = 0; i < numbers.length; i++) {System.out.println(maxnub[i]);}int max = maxnub[0];for (int i = 1; i < maxnub.length; i++) { //计算出最终的最大值,以及确定子序列if (maxnub[i] > max) {max = maxnub[i];s = i;w = end[i];}}System.out.println("最大连续子序列和为:"+max);System.out.println("子序列内容如下:");for (int i = s; i <= w; i++) {System.out.println(numbers[i]);}}}

但是我写的代码还有不足之处,在处理新数组array[1, 4, -5, 9, 8, 3, -6]出现错误
出现错误的不是最大值计算,而是子序列的确定
查看Dubug情况
end存储的结果没有任何问题
问题出在
所以我们添加一行代码 w = end[0];
最终代码为
package com.xy.recursion;
public class xisy {
public static void main(String[] args) {
int s = 0,w = 0;
int numbers[] = {1, 4, -5, 9, 8, 3, -6}; //创建原数组
int[] memory = new int[100]; //备忘录
int[] maxnub = new int[numbers.length]; //存储最大值
int[] end = new int[numbers.length];//子序列最后的位置
for (int i = 0; i < numbers.length; i++) {
maxnub[i] = numbers[i];
memory[i] = numbers[i];
for (int j = i+1; j < numbers.length; j++) {
memory[j] = memory[j-1]+numbers[j];
if (memory[j] > maxnub[i]) {
maxnub[i] = memory[j];
end[i] = j;
}
}
for (int k = 0; k <= numbers.length; k++) {
memory[k] = 0;
}
}
System.out.println("每个数字对应最大值情况");
for (int i = 0; i < numbers.length; i++) {
System.out.print(maxnub[i]+"\t");
}
System.out.println("\n");
int max = maxnub[0];
w = end[0];
for (int i = 1; i < maxnub.length; i++) {
if (maxnub[i] > max) {
max = maxnub[i];
s = i;
w = end[i];
}
}
System.out.println("最大连续子序列和为:"+max);
System.out.println("子序列内容如下:");
for (int i = s; i <= w; i++) {
System.out.print(numbers[i]+",");
}
}
}

我的代码能力没有训练过,就这一道题花费4小时左右,可见愚钝,但是代码都是我一个一个敲出来的,没有参考谁,难得有此机会,说一说自己的想法。大家应该关心核心代码,对于前面变量和数组的定义不要想为什么你会想到一开始就写这么多,实际上不是这样的,只是写基本要用的,后面需要的再去添加。其次就是Debug的使用,这是非常关键的,今天也是在这里停留一段时间,决定记录一下eclipse的这个功能。再者,我会利用此次代码融会贯通前面的动态规划的思想
Debug功能学习
单击右键
鼠标指向最右侧(蓝色部分),鼠标左键双击实现断点
断点的含义是,执行断点前的代码直到断点处结束
然后,再次执行
最后
点击变量选项
关注 
Resume - Suspend - Terminate - Disconnect - Step Into - Step Over - Step Return
删除Debug测试
点击Run—>Debug Configurations
融会贯通动态规划思想
