Java Minimum and Maximum values in Array

前端 未结 13 1021
没有蜡笔的小新
没有蜡笔的小新 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: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;iimax){
                    max=arr[ii];
                }
            }
            System.out.println("The minimum in the arr::"+min);
            System.out.println("The maximum in the arr::"+max);
        }
    }
    

提交回复
热议问题