Hello everyone.
Struggling with this java assignment:
Given an array of numbers, every even number in array is distance traveled, every odd number is time traveled. Need to find total distance traveled. Basically need to multiply [0] and [1], [2] and [3] in array and so on, and get the total.
import java.util.*; public class MyClass { public static int odometer(int [] travel) { int N = travel.length; int distancePerHour = 0; int distanceTotal = 0; for (int i = 0; i < N; i += 2) { distancePerHour = travel[i] * travel[i + 1]; distanceTotal += distancePerHour; } return distanceTotal; } public static void main(String[] args) { int [] testarr = {20,2,30,6,10,7}; System.out.println(odometer(testarr)); } }
So the result im getting is 20*2 + 30*6 + 10*7 = 290
However when i upload this to test server, it returns 170 ! Seems like it's only multiplies last 2 elements of the array. Need second opinion whether my code is working correctly. Many thanks.