个人作业2

设计思路:首先从文件读入一个字符串数组,然后转化为int数组,再进行判断

源程序代码:

package classone;

import java.io.*;
import java.util.Arrays;

/**
 * Created by Nickwong on 31/07/2018.
 * 根据1-8楼的建议,优化了代码
 */
public class Test2 {
    
    static int number=11;
    static String[] t0=new String[number];
    static int[] t1 = new int[number];
    static int[] t2 = new int[number];                //从第几个开始子数组
    static int[] t3 = new int[number];                            //从第几个开始子数组之和
    public static void main(String args[]) {
        readFile();
        
        
        for(int i=0;i<10;i++){
            String b=t0[i];
            int a = Integer.parseInt(b);
            t1[i]=a;
            System.out.println(a);
        }
        
        vs();
       
//        writeFile();
    }

    /**
     * 读入TXT文件
     */
    public static void readFile() {
        
        String pathname = "D:\\file.txt"; // 绝对路径或相对路径都可以,写入文件时演示相对路径,读取以上路径的input.txt文件
        //防止文件建立或读取失败,用catch捕捉错误并打印,也可以throw;
        //不关闭文件会导致资源的泄露,读写文件都同理
        //Java7的try-with-resources可以优雅关闭文件,异常时自动关闭文件;详细解读https://stackoverflow.com/a/12665271
        try (FileReader reader = new FileReader(pathname);
             BufferedReader br = new BufferedReader(reader) // 建立一个对象,它把文件内容转成计算机能读懂的语言
        ) {
            String line;
            int i=0;
            //网友推荐更加简洁的写法
            while ((line = br.readLine()) != null) {
                // 一次读入一行数据
                t0[i]=line;
                i++;
                
//                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 写入TXT文件
     */
//    public static void writeFile() {
//        try {
//            File writeName = new File("output.txt"); // 相对路径,如果没有则要建立一个新的output.txt文件
//            writeName.createNewFile(); // 创建新文件,有同名的文件的话直接覆盖
//            try (FileWriter writer = new FileWriter(writeName);
//                 BufferedWriter out = new BufferedWriter(writer)
//            ) {
//                out.write("我会写入文件啦1\r\n"); // \r\n即为换行
//                out.write("我会写入文件啦2\r\n"); // \r\n即为换行
//                out.flush(); // 把缓存区内容压入文件
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
    public static void vs() {
        int max=0;//比较
        int b=t1[1];
        t2[1]=0;
        for(int i=1;i<=10;i++) {
            int num=0;
            for(int j=i;j<=10;j++) {
                num+=t1[j];
                t2[j]=num;
                for(int s=1;s<=10;s++)
                {
                    if(t2[j-1]<t2[j]) {
                        max=t2[j];
                    }
                }
                t3[i]=max;
            }
            for(int a=1;a<=10;a++) {
                if(t3[a-1]<t3[a]) {
                    b=t3[a];
                }
            }
        }
        System.out.println("\n"+"子数组之和最大值为:"+b);
    }
 
}

结果截图:

package classone;
import java.io.*;import java.util.Arrays;
/** * Created by Nickwong on 31/07/2018. * 根据1-8楼的建议,优化了代码 */public class Test2 {static int number=11;static String[] t0=new String[number];    static int[] t1 = new int[number];    static int[] t2 = new int[number];//从第几个开始子数组    static int[] t3 = new int[number];//从第几个开始子数组之和    public static void main(String args[]) {        readFile();                        for(int i=0;i<10;i++){        String b=t0[i];        int a = Integer.parseInt(b);        t1[i]=a;        System.out.println(a);        }                vs();       //        writeFile();    }
    /**     * 读入TXT文件     */    public static void readFile() {            String pathname = "D:\\file.txt"; // 绝对路径或相对路径都可以,写入文件时演示相对路径,读取以上路径的input.txt文件        //防止文件建立或读取失败,用catch捕捉错误并打印,也可以throw;        //不关闭文件会导致资源的泄露,读写文件都同理        //Java7的try-with-resources可以优雅关闭文件,异常时自动关闭文件;详细解读https://stackoverflow.com/a/12665271        try (FileReader reader = new FileReader(pathname);             BufferedReader br = new BufferedReader(reader) // 建立一个对象,它把文件内容转成计算机能读懂的语言        ) {            String line;            int i=0;            //网友推荐更加简洁的写法            while ((line = br.readLine()) != null) {                // 一次读入一行数据            t0[i]=line;            i++;            //                System.out.println(line);            }        } catch (IOException e) {            e.printStackTrace();        }    }
    /**     * 写入TXT文件     *///    public static void writeFile() {//        try {//            File writeName = new File("output.txt"); // 相对路径,如果没有则要建立一个新的output.txt文件//            writeName.createNewFile(); // 创建新文件,有同名的文件的话直接覆盖//            try (FileWriter writer = new FileWriter(writeName);//                 BufferedWriter out = new BufferedWriter(writer)//            ) {//                out.write("我会写入文件啦1\r\n"); // \r\n即为换行//                out.write("我会写入文件啦2\r\n"); // \r\n即为换行//                out.flush(); // 把缓存区内容压入文件//            }//        } catch (IOException e) {//            e.printStackTrace();//        }//    }    public static void vs() {    int max=0;//比较    int b=t1[1];    t2[1]=0;    for(int i=1;i<=10;i++) {    int num=0;    for(int j=i;j<=10;j++) {    num+=t1[j];    t2[j]=num;    for(int s=1;s<=10;s++)    {    if(t2[j-1]<t2[j]) {    max=t2[j];    }    }    t3[i]=max;    }    for(int a=1;a<=10;a++) {    if(t3[a-1]<t3[a]) {    b=t3[a];    }    }    }    System.out.println("\n"+"子数组之和最大值为:"+b);} }

posted @ 2019-03-17 19:59  雾霾王者  阅读(94)  评论(0编辑  收藏  举报