Java笔试题总结(三)

package com.lee;
//一个数组中三个数相加,求最大的值是多少,且数组的下标
public class threeNumberAdd {
    int[] temp;
    int temp2;
    int k;
    public void v(int[] a) {
        int n = a.length;
        int[] temp = new int[a.length-1];
        for (int i = 0; i <= n-3; i++) {
            System.out.println(i+2);
            temp[i] = a[i] + a[i + 1] + a[i + 2];
        }
        System.out.println("=="+temp.length);
        for(int i=0;i<temp.length-1;i++){
            //找出最大的一个
            if(temp[i]<temp[i+1]){
                temp2=temp[i];
                temp[i]=temp[i+1];
                temp[i+1]=temp2;
                k=i+1;
            }
        }
        System.out.println("最大的值:"+temp2+" |位置在:"+k);
    }
    public static void main(String[] args){
        threeNumberAdd tna=new threeNumberAdd();
        int[] a={1,2,3,5,1};
        System.out.println(a.length);
        tna.v(a);

    }

}


你可能感兴趣的:(Java笔试题总结(三))