找到最大和最小的二维数组

时间:2016-03-20 20:39:46

标签: java

这是原始提示:

  

在milesTracker中找到最大值和最小值。将最大值分配给maxMiles,将最小值分配给minMiles。给定程序的示例输出:

Min miles: -10 
Max miles: 40

这是我的代码:

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; 
    int minMiles = 0; 

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

    for(i=0;i<NUM_ROWS;++i) {

       for(j=0;j<NUM_COLS;++j) {

          if (milesTracker[i][j]<minMiles){
              minMiles = milesTracker[i][j]; 
          }
          else if (milesTracker[i][j] > maxMiles){
              maxMiles = milesTracker[i][j];
          }
       } 
    }

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

这是输出:

  Testing with milesTracker = {{-10, 20}, {30, 40}}

你的输出:

Min miles: -10
Max miles: 40

   Testing with milesTracker = {{73, 0}}

你的输出:

Min miles: 0
Max miles: 73

✖   Testing with milesTracker = {{-5}, {-93}, {-259}}

预期产出:

Min miles: -259
Max miles: -5

你的输出:

Min miles: -259
Max miles: 0

为什么最后一次测试失败?

7 个答案:

答案 0 :(得分:5)

您需要以不同方式初始化初始值。

int maxMiles = Integer.MIN_VALUE; 
int minMiles = Integer.MAX_VALUE; 

将它们初始化为0会导致问题,因为您可以在示例中看到矩阵中只有负值。如果您只有正数,minMiles将保持为0并且错误,因为您永远不会得到小于初始值0的值。

对于minValue,您需要确保数组中的任何内容都小于初始值,因此您可以为其指定所用类型的最大可能值。对于maxValue,情况正好相反。

另一个可能的错误是else if条件。

if (milesTracker[i][j] < minMiles) {
    minMiles = milesTracker[i][j]; 
} else if (milesTracker[i][j] > maxMiles) {
    maxMiles = milesTracker[i][j];
}

这两者并不相互排斥。如果您的广告数量大于当前maxMiles且小于当前minMiles,那该怎么办?在您的情况下,它肯定会发生并且您无法更新其中一个 - maxMiles

答案 1 :(得分:1)

作为@Fikolev答案的补充:如果只允许编辑循环体,可以在那里移动初始化:

for(i=0;i<NUM_ROWS;++i) {
  for(j=0;j<NUM_COLS;++j) {
    if (i == 0 && j == 0) {
      maxMiles = Integer.MIN_VALUE; 
      minMiles = Integer.MAX_VALUE; 
    }
  ...

答案 2 :(得分:1)

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

        if (milesTracker[i][j] > maxMiles) {
            maxMiles = milesTracker[i][j];
        }

        else if (milesTracker[i][j] < minMiles) {
            minMiles = milesTracker[i][j];
        }

    }
}

答案 3 :(得分:0)

这是我使用的。

 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;

      /* Your solution goes here  */
      for (i = 0; i < NUM_ROWS; i++) {

         for (j = 0; j < NUM_COLS; j++) {

            if (i == 0 && j == 0) {
               maxMiles = milesTracker[i][j];
               minMiles = milesTracker[i][j];
            }

               if (milesTracker[i][j] > maxMiles) {
                  maxMiles = milesTracker[i][j];
               }

               else if (milesTracker[i][j] < minMiles) {
                 minMiles = milesTracker[i][j];
               }

            }

         }

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

答案 4 :(得分:0)

在循环前的mileTracker中使用第一个元素进行分配

maxMiles = milesTracker[0][0];
minMiles = milesTracker[0][0];

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

答案 5 :(得分:0)

@Filkolev的答案非常好,但你可以更短:

//The solution requires students to initialize max_miles (or maxMiles) or and min_miles (or minMiles) before writing their nested for loop. 
maxMiles = milesTracker[0][0];
minMiles = milesTracker[0][0];

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

答案 6 :(得分:-1)

在开始for循环之前插入数组坐标:

EX:

minMiles=milesTracker[0][0];
maxMiles=milesTracker[0][0];

for(i...){max
    for(j...){min