int cannot be converted to int []

后端 未结 4 720
陌清茗
陌清茗 2021-01-24 02:39

new to programming here and i keep getting the error message, \'incompatible types, int cannot be converted to int []\', the question is to add R1& R2 together if they are o

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-24 03:00

    Actually in that line sumArray is an integer array and you are assigning it as integer only and also you haven't declared variable j. Try this-

    public int[] arrayAdd(int[] R1, int[] R2)
    {
        int[] sumArray= new int[R1.length];
    
        if( R1.length!= R2.length)
        {
            System.out.println("The arrays must be same length");
        }
        else
        {
            for(int i=0; i< R1.length; i++)
    
            {
    
                sumArray[i]= R1[i]+ R2[i]; // Error
            }
        }
        return sumArray;
    }
    

提交回复
热议问题