• 数组测试 --Junit


    1、添加依赖

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })
        compile 'com.android.support:appcompat-v7:25.1.1'
        compile 'com.android.support.constraint:constraint-layout:1.0.1'
        testCompile 'junit:junit:4.12'
    }

    2.java代码

    package com.example.testforgit;

    /**
     * Created by ** on 2017/3/13.
     */

    public class Work {
        public static int forsum(int[] array){
            if(array.length==0 || array==null){
                return 0;
            }
            int Sum1 = 0;//存储当前连续n项的和
            int max =0;//存储连续子元素和的最大值
            for(int i=0;i<array.length;i++){
                if(Sum1<=0){
                    Sum1=array[i];
                }else{
                    Sum1+=array[i];
                }
                if(Sum1>max){
                    max=Sum1;
                }
            }
            return max;
        }
    }

    3.测试部分

    package com.example.testforgit;

    import org.junit.Before;
    import org.junit.Test;

    import static org.junit.Assert.*;

    /**
     * Created by 金林 on 2017/3/13.
     */
    public class WorkTest {
        private Work mwork;
        @Before
        public void setUp() throws Exception {
                mwork = new Work();
        }

        @Test
        public void forsum() throws Exception {
            int[] array={-1,2,3,-4};
            int result = 5;
            result=mwork.forsum(array);
            System.out.print(result);

        }


    }

  • 相关阅读:
    【转】Shell编程基础篇-上
    【转】inotify+rsync实现实时同步
    Spring
    jdk,jre,tommcat配置问题
    Java前后台开发
    前端组件学习(一)
    报表工具进阶(二)
    查询时异步刷新问题--用到了ajax
    学习jaspersoft/JasperReport
    利用SQLYog操作数据库mysql
  • 原文地址:https://www.cnblogs.com/jinlindb/p/6567015.html
Copyright © 2020-2023  润新知