课堂作业——返回整数数组最大子数组(第一次修改:解决之前注释中说明的问题)


package com.Cra2iTeT;

import java.io.*;
import java.util.Scanner;

/**
 * @author Cra2iTeT
 * @version 1.0
 * @date 2022/3/11 11:00
 */
public class test02 {
    public static void main(String[] args) {
        try {
            Scanner scanner = new Scanner(new FileReader("src/com/Cra2iTeT/test02.txt"));

            int row = 0;
            int column = 0;
            int tempRow, tempColumn;

            for (int i = 0; i < 2 && scanner.hasNextLine(); i++) {
                String line = scanner.nextLine();
                if (i == 0) {
                    row = Integer.parseInt(line.substring(0, line.indexOf(',')));
                } else {
                    column = Integer.parseInt(line.substring(0, line.indexOf(',')));
                }
            }

            if (row <= 0 || column <= 0) {
                System.out.println("行数或者列数不对");
                return;
            }

            int tempInt[] = new int[row * column];
            int tempIndex = 0;

            for (tempRow = 0; tempRow < row && scanner.hasNextLine(); tempRow++) {  //不存在下一行或者已经读完该读的行数
                String line = scanner.nextLine();

                for (tempColumn = 0; tempColumn < column - 1 && !line.equals(""); tempColumn++) {
                    tempInt[tempIndex++] = Integer.parseInt(line.substring(0, line.indexOf(',')));  //先获取从头开始至第一个逗号之间的数
                    line = line.substring(line.indexOf(',') + 1, line.length());    //将第一个逗号之后的值覆盖原来的值
                }

                if (!line.equals("")) { //最后一个数因为没有逗号所以需要单独判断(有些缺陷,万一给的数据错误,正常应该结束的位置没有结束,后面还有数据的话读数据应该会有错,只是猜想,没尝试)
                    if (line.indexOf(',') != -1) {
                        System.out.println("数据对应错误,第" + (tempRow + 1) + "行的列数不对"); //后续还有数据
                        return;
                    } else {
                        tempInt[tempIndex++] = Integer.parseInt(line.substring(0)); //后续没有数据
                    }
                    tempColumn++;
                }

                if (tempColumn != column) {
                    System.out.println("数据列数不对");
                    return;
                }
            }

            if (tempRow != row || scanner.hasNextLine()) {
                System.out.println("数据行数不对");
                return;
            }

            int max = 0;
            int tempMax = 0;

            for (int i = 0; i < row * column; i++) {
                if (i == 0) {
                    tempMax = tempInt[i];
                    max = tempInt[i];
                } else {
                    if (tempMax < 0) {
                        tempMax = tempInt[i];
                    } else {
                        tempMax += tempInt[i];
                    }
                }

                if (tempMax > max) {
                    max = tempMax;
                }
            }

            System.out.println(max);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

也还是不完全对,没有考虑单个数值超出int的情况