vendredi 26 mai 2017

Getting the minimum and maximum values in a randomly generated array in java

import java.util.Random;


    public class Java 
    {
        public static void main(String args[])
        {

        int arr[] =  new int[10];
        Random Rand = new Random();


        int max=0;
        int min=arr[0];


        for(int i=0; i<10; i++)
        {
            int RandNum = Rand.nextInt(10);
            arr[i] = RandNum;


            if(i==0) { System.out.print("["); } 

            System.out.print(arr[i] + " "); 

            if(i==9) { System.out.println("]"); }


            if(RandNum>max)
            {
                max = RandNum;
            }


            if(i>0)
            {
                if(RandNum<arr[0])
                {
                    min = RandNum;
                }
            }

        }

        System.out.println("Minimum number: " + min);
        System.out.println("Maximum number: " + max);   

        } 
    }

I could get the maximum value, but not the minimum.. please bear with me, I'm a beginner, I've tried comparing the min value with the first array value, but I'm not sure if the min value should be array[0] or just 0.




Aucun commentaire:

Enregistrer un commentaire