Ask Question
25 April, 21:42

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, 95Note that the last element is not followed by a comma, space, or newline. import java. util. Scanner; public class PrintWithComma {public static void main (String [] args) {final int NUM_VALS = 4; int[] hourlyTemp = new int[NUM_VALS]; int i = 0; hourlyTemp[0] = 90; hourlyTemp[1] = 92; hourlyTemp[2] = 94; hourlyTemp[3] = 95; / * Your solution goes here * / System. out. println (""); return; }}

+1
Answers (1)
  1. 26 April, 00:25
    0
    Here are the for loop for the given question.

    for (i = 0; i < NUM_VALS; i++) / / for loop

    {

    / * check if the value of i is equal to NUM_VALS or not. If it is

    equal then just print the value without comma.*/

    if (i= = (NUM_VALS-1)) / / if block

    System. out. print (hourlyTemp[i]);

    / * if i is not equal then just print the value with comma.*/

    else / / else block

    System. out. print (hourlyTemp[i] + ", ");

    }

    Explanation:

    In this we iterating the for loop and check the condition

    check if the value of i is equal to NUM_VALS or not. If it is equal then just print the value without comma

    If i is not equal then just print the value with comma.

    Output:

    90,92,94,95
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “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, ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers