Given the integer array hourlyMiles with the size of ARR_VALS, write a for loop that sets sumOdds
Question:
Given the integer array hourlyMiles with the size of ARR_VALS, write a for loop that sets sumOdds to the sum of all the odd integers in hourlyMiles.
Ex: If the input is 64 75 53 106 80 50 52 118 109 79
, then the output is
import java.util.Scanner;
public class OddSum {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
final int ARR_VALS = 10;
int[] hourlyMiles = new int[ARR_VALS];
int i;
int sumOdds;
for (i = 0; i < hourlyMiles.length; ++i) {
hourlyMiles[i] = scnr.nextInt();
}
for (i = 0; i < hourlyMiles.length; ++i) {
if(hourlyMiles[i] % 2 != 0)
{
sumOdds += hourlyMiles[i];
}
}
System.out.println("Odd sum: " + sumOdds);
}
}
OddSum.java:18: error: variable sumOdds might not have been initialized sumOdds += hourlyMiles[i]; ^ OddSum.java:22: error: variable sumOdds might not have been initialized System.out.println("Odd sum: " + sumOdds); ^ 2 errors
What is wrong with my code?