java 查找二维数组最大值和最小值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36119278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find 2D array max and min
提问by java2019
This is the original prompt:
这是原来的提示:
Find the maximum value and minimum value in milesTracker. Assign the maximum value to maxMiles, and the minimum value to minMiles. Sample output for the given program:
Min miles: -10 Max miles: 40
在milesTracker中找到最大值和最小值。将最大值分配给 maxMiles,将最小值分配给 minMiles。给定程序的示例输出:
Min miles: -10 Max miles: 40
Here is my code:
这是我的代码:
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);
}
}
Here is the output:
这是输出:
Testing with milesTracker = {{-10, 20}, {30, 40}}
Your output:
你的输出:
Min miles: -10
Max miles: 40
Testing with milesTracker = {{73, 0}}
Your output:
你的输出:
Min miles: 0
Max miles: 73
? Testing with milesTracker = {{-5}, {-93}, {-259}}
Expected output:
预期输出:
Min miles: -259
Max miles: -5
Your output:
你的输出:
Min miles: -259
Max miles: 0
Why is the last test failing?
为什么最后一次测试失败?
回答by Filkolev
You need to initialize your initial values differently.
您需要以不同的方式初始化初始值。
int maxMiles = Integer.MIN_VALUE;
int minMiles = Integer.MAX_VALUE;
Initializing them both to 0 leads to problems as you can see in the example with only negative values in the matrix. If you have only positive numbers, minMiles
will stay 0 and will be wrong as you'll never get a value smaller than the initial 0.
将它们都初始化为 0 会导致问题,如您在矩阵中只有负值的示例中所见。如果您只有正数,minMiles
则将保持 0 并且是错误的,因为您永远不会得到小于初始 0 的值。
For minValue
you need to make sure that whatever you have in the array is smaller than the initial value, hence you assign it the maximal possible value of the type you use. For maxValue
it's the opposite.
因为minValue
您需要确保数组中的任何内容都小于初始值,因此您将其分配给您使用的类型的最大可能值。因为maxValue
它是相反的。
Another possible error is the else if
condition.
另一个可能的错误是else if
条件。
if (milesTracker[i][j] < minMiles) {
minMiles = milesTracker[i][j];
} else if (milesTracker[i][j] > maxMiles) {
maxMiles = milesTracker[i][j];
}
These two are not mutually exclusive. What if you reach a number that is both larger than your current maxMiles
and smaller than the current minMiles
? It can certainly happen and you'll fail to update one of them, in your case - maxMiles
.
这两者并不相互排斥。如果您达到一个既大于当前值maxMiles
又小于当前值的数字minMiles
怎么办?它肯定会发生,并且您将无法更新其中之一,在您的情况下 - maxMiles
。
回答by choeger
As an addition to @Fikolev's answer: If you are only allowed to edit the loop bodies, you can move the initialization there:
作为@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;
}
...
回答by Lubrious
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];
}
}
}
回答by jam
Assign with first element in milesTracker before loop
在循环之前分配milesTracker中的第一个元素
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];
}
}
}
回答by Cosmopus
The answer of @Filkolev is really good, but you can be even shorter:
@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];
}
}
}
回答by codeN00b
Here is what I used.
这是我使用的。
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);
}
}
回答by Mikes
Insert array coordinates before you start your for loops:
在开始 for 循环之前插入数组坐标:
EX:
前任:
minMiles=milesTracker[0][0];
maxMiles=milesTracker[0][0];
for(i...){max
for(j...){min