Java Minimum and Maximum values in Array

前端 未结 13 791
没有蜡笔的小新
没有蜡笔的小新 2020-12-01 06:32

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[]          


        
相关标签:
13条回答
  • 2020-12-01 07:25
    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-01 07:28

    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.

    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 讨论(0)
  • 2020-12-01 07:30

    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.

    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 讨论(0)
  • 2020-12-01 07:35

    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]);
    
    0 讨论(0)
  • 2020-12-01 07:35

    Sum, Maximum and Minimum value of an Array in One Line

     public static void getMinMaxByArraysMethods(int[] givenArray){
    
           //Sum of Array in One Line 
           long sumofArray =  Arrays.stream(givenArray).sum();
    
           //get Minimum Value in an array in One Line
            int minimumValue = Arrays.stream(givenArray).min().getAsInt();
    
            //Get Maximum Value of an Array in One Line
            int MaxmumValue =  Arrays.stream(givenArray).max().getAsInt();
    
        }
    
    0 讨论(0)
  • 2020-12-01 07:38

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题