In: Computer Science
Write a for loop to print all NUM_VALS elements of array hourlyTemp. Separate elements with a comma and space. Ex: If hourlyTemp = {90, 92, 94, 95}, print:
90, 92, 94, 95
Your code's output should end with the last element, without a subsequent comma, space, or newline.
Code:-----------
public class Main {
public static void main (String [] args) {
final int NUM_VALS = 4;
int[] hourlyTemp = new int[NUM_VALS];
int i;
hourlyTemp[0] = 90;
hourlyTemp[1] = 92;
hourlyTemp[2] = 94;
hourlyTemp[3] = 95;
/* Your solution goes here */
for(i = 0; i < hourlyTemp.length; ++i){
if(i < hourlyTemp.length - 1){
System.out.print(hourlyTemp[i] + ", ");
}
else{
System.out.print(hourlyTemp[i]);
}
}
System.out.println("");
}
}
Screenshot:--------------
