1. Create an int array named a with 1000 elements, and give the ith element the value i. 
2. Compute the sum of all the elements with an even index. 
3. Compute the sum of all the elements with an odd index. 所有的代码都是要求写在Interaction里面的
我只会做第一步,而且还觉得不对,个人觉得应该是{1,2,3...1000},请帮我看看对不对:
> int[] a= new int[1000]
> for(int i= 1; i<a.length; i++ )
a[i]= i;
> a
{ 0, 1, 2, 3, 4, 5, ...999 }二三步都不会做,麻烦帮忙看看,谢谢

解决方案 »

  1.   


    package Test;public class TTT { public static void main(String[] args) {
    TTT.interaction();
    }

    public static void interaction(){
    int [] a = new int [1000];
    int even_sum = 0;
    int odd_sum = 0;
    for(int i = 0; i < 1000; i++){
    int value = i+1;
    a[i] = value;
    if(value%2 == 0)
    even_sum += value;
    else
    odd_sum += value;
    }
    System.out.println("the sum of even is :"+even_sum);
    System.out.println("the sum of odd is :"+odd_sum);
    }
    }
      

  2.   

    不是,它必须要在Interaction里面写
    也就是下面的测试窗口写,而不是上面的Constructor窗口写
      

  3.   

    public class Test {    public static void main(String[] args) {
         Test.interaction();
        }
        
        public static void interaction(){
            int [] a = new int [1000];
            int even_sum = 0;
            int odd_sum = 0;
            for(int i = 1; i < a.length+1; i=i+2){
             odd_sum += i;
               even_sum += i+1;
            }
            System.out.println("the sum of even is :"+even_sum);
            System.out.println("the sum of odd is :"+odd_sum);
        }
    }