Java 查找 for 循环中值的最大值/最小值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33073179/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:16:47  来源:igfitidea点击:

Find the Max/Min of values within a for loop

javafor-loop

提问by Eyzek

I've searched for answers to this specific question, but haven't been able to find anything. I need to find the maximum and minimum of the input numbers but the values I need are inside the for loop and I can't figure out how to use them outside of it.

我已经搜索了这个特定问题的答案,但没有找到任何东西。我需要找到输入数字的最大值和最小值,但我需要的值在 for 循环内,我无法弄清楚如何在循环之外使用它们。

System.out.print("How many numbers do you want to input?");

    int totalNumbers = console.nextInt();

    int minMax = 0;
    for(int i = 1; i <= totalNumbers; i++){
        System.out.print("Number " + i + ": ");
        int inputNumbers = console.nextInt();
    }
    System.out.println();

    int smallest = Math.min(minMax);
    System.out.println("Smallest = " + smallest);
    int largest = Math.max(minMax); 
    System.out.println("Largest = " + largest);

I don't need changed code just something that will get me on the right track.Thank you!

我不需要更改代码,只要能让我走上正轨就行了。谢谢!

采纳答案by akhil_mittal

Can you notice the problem with the following loop?

你能注意到以下循环的问题吗?

for(int i = 1; i <= totalNumbers; i++){
        System.out.print("Number " + i + ": ");
        int inputNumbers = console.nextInt();
    }

You are running the loop totalNumberstimes and every time you create a new intwith name inputNumbersand store the value received from console. Also where are you changing the value of minMax? Also Math.min(or max)does not take single paramtere and wont even compile.

您正在运行循环totalNumbers时间,并且每次int使用名称创建一个新的inputNumbers并存储从控制台收到的值。另外你在哪里改变的价值minMax?也Math.min(or max)没有采取单一paramtere和甚至不会编译。

Now you have few options:

现在你有几个选择:

  1. Either store all the numbers in an array and then traverse that for min and max elements using some utility method.
  2. Set some min and max value and run a loop to get all items and also keep track of min and max in loop.
  1. 要么将所有数字存储在一个数组中,然后使用某种实用方法遍历 min 和 max 元素。
  2. 设置一些最小值和最大值并运行一个循环来获取所有项目,并在循环中跟踪最小值和最大值。

I am not writing any solution as I want you to try it yourself.

我没有写任何解决方案,因为我希望您自己尝试。

回答by Singular1ty

Unless I'm misreading what you want, can't you just maintain a variable on the outside of the for loop and check them?

除非我误读了您想要的内容,否则您不能在 for 循环的外部维护一个变量并检查它们吗?

int minMax = 0;
int smallest = 0;
int largest = 0;

for(int i = 1; i <= totalNumbers; i++){
    System.out.print("Number " + i + ": ");
    int inputNumbers = console.nextInt();
    if(inputNumbers > largest){
        largest = inputNumbers;
    } else if (inputNumbers < smallest){
        smallest = inputNumbers;
   }
}
System.out.println();

System.out.println("Smallest = " + smallest);
System.out.println("Largest = " + largest);

This is the most direct and logical way of checking the input values and deciding whether they're the smallest or largest currently known (Edit:Unless you requirethe use of Math.minMax)

这是检查输入值并确定它们是当前已知的最小还是最大的最直接和合乎逻辑的方法(编辑:除非您需要使用Math.minMax

回答by Ranveer

You can do it by just put if else condition inside for loop... like

您可以通过将 if else 条件放在 for 循环中来实现...就像

  1. take 2 variable at outside loop one for max value and other for min value store.
  2. inside loop assign input number to min and max first time.
  3. after that comare next number with this and reassign values.
  4. at end of loop you will find both min and max.
  1. 在外部循环中取 2 个变量,第一个用于最大值,另一个用于最小值存储。
  2. 内部循环第一次将输入编号分配给 min 和 max。
  3. 之后用这个comare下一个数字并重新分配值。
  4. 在循环结束时,您将找到最小值和最大值。

回答by DaoWen

Pseudocode:

伪代码:

smallest := +∞
largest  := -∞

for each number read, n:
    if n > largest:
        largest := n
    if n < smallest:
        smallest := n

print the results

Hint:

暗示:

Java ints can't represent ±∞. Use Integer.MIN_VALUEand Integer.MAX_VALUEinstead.

Java ints 不能代表±∞。使用Integer.MIN_VALUEInteger.MAX_VALUE代替。

回答by Schuyler Reinken

The Math.min() and Math.max() methods, according to the oracle documentation, can only compare two values. Importing the values into an array, and then performing operations on the array, should allow you to find minimums and maximums, as well as any other data operation quite easily.

Math.min() 和 Math.max() 方法,根据 oracle 文档,只能比较两个值。将值导入数组,然后对数组执行操作,应该可以让您轻松找到最小值和最大值,以及任何其他数据操作。

int[] numbers = new int[totalNumbers];

for (int i = 0; i < totalNumbers; i++) {
    numbers[i] = console.nextInt();
}

//Other Operations