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

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?

Step by Step Solution

3.52 Rating (162 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

The issue with your code is that you havent initiali... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!