Java 数组中的最小值和最大值

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

Java Minimum and Maximum values in Array

javaarraysmaxminimum

提问by user2673161

My code does not give errors, however it is not displaying the minimum and maximum values. The code is:

我的代码没有给出错误,但是它没有显示最小值和最大值。代码是:

Scanner input = new Scanner(System.in);

int array[] = new int[10];

System.out.println("Enter the numbers now.");

for (int i = 0; i < array.length; i++) {
    int next = input.nextInt();
    // sentineil that will stop loop when 999 is entered
    if (next == 999) {
        break;
    }
    array[i] = next;
    // get biggest number
    getMaxValue(array);
    // get smallest number
    getMinValue(array);
}

System.out.println("These are the numbers you have entered.");
printArray(array);


// getting the maximum value
public static int getMaxValue(int[] array) {
    int maxValue = array[0];
    for (int i = 1; i < array.length; i++) {
        if (array[i] > maxValue) {
            maxValue = array[i];
        }
    }
    return maxValue;
}

// getting the miniumum value
public static int getMinValue(int[] array) {
    int minValue = array[0];
    for (int i = 1; i < array.length; i++) {
        if (array[i] < minValue) {
            minValue = array[i];
        }
    }
    return minValue;
}

//this method prints the elements in an array......
//if this case is true, then that's enough to prove to you that the user input has  //been stored in an array!!!!!!!
public static void printArray(int arr[]) {
    int n = arr.length;

    for (int i = 0; i < n; i++) {
        System.out.print(arr[i] + " ");
    }
}

Do I need a system.out.println() to display it, or should the return work?

我需要一个 system.out.println() 来显示它,还是应该返回工作?

采纳答案by rocketboy

getMaxValue(array);
// get smallest number
getMinValue(array);

You are calling the methods but not using the returned values.

您正在调用方法但未使用返回值。

System.out.println(getMaxValue(array));
System.out.println(getMinValue(array)); 

回答by Dmitry Bychenko

You just throw away Min/Max values:

您只需丢弃最小/最大值:

  // get biggest number
  getMaxValue(array); // <- getMaxValue returns value, which is ignored
  // get smallest number
  getMinValue(array); // <- getMinValue returns value, which is ignored as well

You can do something like

你可以做类似的事情

  ... 
  array[i] = next;

  System.out.print("Max value = ");
  System.out.println(getMaxValue(array)); // <- Print out getMaxValue value

  System.out.print("Min value = ");
  System.out.println(getMinValue(array)); // <- Print out getMinValue value

  ...  

回答by Prabhaker A

You are doing two mistakes here.
1. calling getMaxValue(),getMinValue()methods before array initialization completes.
2.Not storing return value returned by the getMaxValue(),getMinValue()methods.
So try this code

你在这里犯了两个错误。
1.getMaxValue(),getMinValue()在数组初始化完成之前调用方法。
2.不存储方法返回的返回值getMaxValue(),getMinValue()
所以试试这个代码

   for (int i = 0 ; i < array.length; i++ ) 
  {
       int next = input.nextInt();
       // sentineil that will stop loop when 999 is entered
       if (next == 999)
       break;
       array[i] = next;
  }
  // get biggest number
  int maxValue = getMaxValue(array);
  System.out.println(maxValue );

  // get smallest number
  int minValue = getMinValue(array);
  System.out.println(minValue);

回答by Korean_Karue

your maximum, minimum method is right

你的最大、最小方法是对的

but you don't print int to console!

但你不打印 int 到控制台!

and... maybe better location change (maximum, minimum) methods

和...也许更好的位置更改(最大,最小)方法

now (maximum, minimum) methods in the roop. it is need not.. just need one call

现在(最大,最小)方法。不需要..只需要一个电话

i suggest change this code

我建议更改此代码

    for (int i = 0 ; i < array.length; i++ ) {
       int next = input.nextInt();
       // sentineil that will stop loop when 999 is entered
       if (next == 999)
       break;
       array[i] = next;
}
System.out.println("max Value : " + getMaxValue(array));
System.out.println("min Value : " + getMinValue(array));
System.out.println("These are the numbers you have entered.");
printArray(array);

回答by clcto

Yes you need to use a System.out.println. But you are getting the minimum and maximum everytime they input a value and don't keep track of the number of elements if they break early.

是的,您需要使用System.out.println. 但是您每次输入一个值时都会获得最小值和最大值,并且如果它们提前中断,则不会跟踪元素的数量。

Try:

尝试:

for (int i = 0 ; i < array.length; i++ ) {
       int next = input.nextInt();
       // sentineil that will stop loop when 999 is entered
       if (next == 999)
           break;

       array[i] = next;
 }
 int length = i;
 // get biggest number
 int large = getMaxValue(array, length);
 // get smallest number
 int small = getMinValue(array, length);

 // actually print
 System.out.println( "Max: " + large + " Min: " + small );

Then you will have to pass length into the methods to determine min and max and to print. If you don't do this, the rest of the fields will be 0 and can mess up the proper min and max values.

然后,您必须将长度传递给方法以确定最小值和最大值并进行打印。如果您不这样做,其余字段将为 0,并且可能会弄乱正确的最小值和最大值。

回答by Ruchira Gayan Ranaweera

You can try this too, If you don't want to do this by your method.

你也可以试试这个,如果你不想用你的方法来做到这一点。

    Arrays.sort(arr);
    System.out.println("Min value "+arr[0]);
    System.out.println("Max value "+arr[arr.length-1]);

回答by Satheeq

Here you haven't print the max and min values. Print the max and min values in the getMaxVal and getMin val methods or after the call. This is the output.

在这里,您还没有打印最大值和最小值。在 getMaxVal 和 getMin val 方法中或在调用之后打印最大值和最小值。这是输出。

Enter the numbers now.
5
Max: 5
Min: 0
3
Max: 5
Min: 0
7
Max: 7
Min: 0
3
Max: 7
Min: 0
90
Max: 90
Min: 0
43
Max: 90
Min: 0
100
Max: 100
Min: 0
45
Max: 100
Min: 0
23
Max: 100
Min: 0
22
Max: 100
Min: 3
These are the numbers you have entered.
5 3 7 3 90 43 100 45 23 22

Also when you are declaring an array, it has all 0s initially.

此外,当您声明一个数组时,它最初全为 0。

回答by kamil.rak

Imho one of the simplest Solutions is: -

恕我直言,最简单的解决方案之一是:-

//MIN NUMBER
Collections.sort(listOfNumbers);
listOfNumbers.get(0);

//MAX NUMBER
Collections.sort(listOfNumbers);
Collections.reverse(listOfNumbers);
listOfNumbers.get(0);

回答by Rahul Sah

Here is the working code to find the min and max in the array.I hope you will find it helpful:

这是在数组中查找最小值和最大值的工作代码。我希望你会发现它有帮助:

 import java.util.Random;
 import java.util.Scanner;
 public class FindMin {
    public static void main(String[] args){
        System.out.println("Main Method Started");
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the size of the arr");
        int size = in.nextInt();
        System.out.println("Enter the maximum value of the arr");
        int max = in.nextInt();
        int [] arr  = initializeArr(max, size);
        print(arr);
        findMinMax(arr);
        System.out.println("Main Method Ended");
    }
    public static void print(int[] arr){
        for(int val:arr){
            System.out.print(val + " ");
        }
        System.out.println();
    }
    public static int[] initializeArr(int max,int size){
        Random random = new Random();
        int [] arr = new int[size];
        for(int ii=0;ii<arr.length;ii++){
            arr[ii]=random.nextInt(max);
        }
        return arr;
    }
    public static void findMinMax(int[] arr){
        int min=arr[0];
        int max=arr[0];
        for(int ii=0;ii<arr.length;ii++){
            if(arr[ii]<min){
                min=arr[ii];
            }
            else if(arr[ii]>max){
                max=arr[ii];
            }
        }
        System.out.println("The minimum in the arr::"+min);
        System.out.println("The maximum in the arr::"+max);
    }
}

回答by user3216114

import java.util.*;
class Maxmin
{
    public static void main(String args[])
    {
        int[] arr = new int[10];
        Scanner in = new Scanner(System.in);
        int i, min=0, max=0;
        for(i=0; i<=arr.length; i++)
        {
            System.out.print("Enter any number: ");
            arr[i] = in.nextInt();          
        }
        min = arr[0];
        for(i=0; i<=9; i++)
        {
            if(arr[i] > max)
            {
                max = arr[i];
            }
            if(arr[i] < min)
            {
                min = arr[i];
            }
        }
        System.out.println("Maximum is: " + max);
        System.out.println("Minimum is: " + min);
    }
}