Find the maximum value and minimum value in below mention code. Assign the maximum value to maxMiles, and the minimum value to minMiles. Sample output for the given program:

Min miles: -10
Max miles: 40
Here's what I have so far:

import java.util.Scanner;

public class ArraysKeyValue {
public static void main (String [] args) {
final int NUM_ROWS = 2;
final int NUM_COLS = 2;
int [][] milesTracker = new int[NUM_ROWS][NUM_COLS];
int i = 0;
int j = 0;
int maxMiles = 0; // Assign with first element in milesTracker before loop
int minMiles = 0; // Assign with first element in milesTracker before loop

milesTracker[0][0] = -10;
milesTracker[0][1] = 20;
milesTracker[1][0] = 30;
milesTracker[1][1] = 40;

//edit from here

for(i = 0; i < NUM_ROWS; ++i){
for(j = 0; j < NUM_COLS; ++j){
if(milesTracker[i][j] > maxMiles){
maxMiles = milesTracker[i][j];
}
}
}
for(i = 0; i < NUM_ROWS; ++i){
for(j = 0; j < NUM_COLS; ++j){
if(milesTracker[i][j] < minMiles){
minMiles = milesTracker[i][j];
}
}
}

//edit to here


System.out.println("Min miles: " + minMiles);
System.out.println("Max miles: " + maxMiles);
}

Respuesta :

Answer:

Code works perfectly

Explanation:

There is nothing wrong with your program as it runs perfectly and displays the expected results.

You may need to compile with another compiler if you're not getting the required results.

I've added the source code as an attachment (unedited)

Ver imagen MrRoyal