0
I’m starting in java, I only had one class. So I’m making a program to add up the items in an array, and I have to use method. This is my code.
Leading:
package exercicio3e4;
public class Principal {
public static void main(String[] args) {
Somar x;
x= new Somar();
x.y={1,4,6,8,1};
x.calcular();
}
}
Add:
package exercicio3e4;
public class Somar {
int[] y;
void calcular() {
int i = y.length;
int total=0;
while (i >= 0) {
i--;
total = total + y[i];
}
System.out.println("Soma da array: " + total);
}
}
Just use
void calcular(int[] x) { ... }
and to callnew Somar().calcular( array )
– Valdeir Psr