zoukankan      html  css  js  c++  java
  • 个人作业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);} }

  • 相关阅读:
    《团队-爬取豆瓣Top250-团队一阶段互评》
    团队-爬虫豆瓣top250项目-开发文档
    结对总结
    课后作业-阅读任务-阅读提问-2
    2017-10-06-构建之法:现代软件工程-阅读笔记
    结对-结对编项目贪吃蛇-开发过程
    团队-爬取豆瓣电影TOP250-开发环境搭建过程
    个人-GIT使用方法
    课后作业-阅读任务-阅读提问-1
    课后作业-阅读任务-阅读笔记-1
  • 原文地址:https://www.cnblogs.com/zlc364624/p/10548541.html
Copyright © 2011-2022 走看看