Witam. Mam problem zadaniem: Elementy pobranej od użytkownika tablicy typu double zwiększ o średnią
elementów z tablicy znajdujących się pod indeksami parzystymi. Tak jak w tytule po zwiększeniu wartości tablicy w konsola wyświetla wartość NaN.
public class Zad3PPZestaw3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
sc.useLocale(US);
int size;
System.out.println("Enter size of array");
size = sc.nextInt();
double[] tab = new double[size];
System.out.println("Enter Elements of array");
for (int i = 0; i < size; i++) {
tab[i] = sc.nextDouble();
sc.nextLine();
}
System.out.println("Array elements after increasing: " + Arrays.toString(increasingOfArraysElements(tab)));
}
public static double average(double[] tab) {
double average = 0;
int elements = 0;
double result = average / elements;
for (int i = 0; i < tab.length; i++) {
if (i % 2 == 0) {
average += tab[i];
elements++;
}
}
return result;
}
public static double[] increasingOfArraysElements(double[] tab) {
for (int i = 1; i < tab.length; i++) {
tab[i] += average(tab);
}
return tab;
}
}
Dziękuje za pomoc w rozwiązaniu problemu. Pozdrawiam