0

Create a program that randomly generates 5 numbers from 1 to 50, have these print out and then a second array to do the same thing. Have the program determine which is the largest number between the 2???? Okay so this is a question i asked myself and this is probably completely wrong but i tried: So this isn't exactly what i want but it's close. I would like it to show the actual highest number.

import java.util.*;
public class TwoArrays
{
public static void main (String args [] )
{
Random r = new Random();
int rangeMin = 0;
int rangeMax = 50;

ArrayList<Double> arrayList1 = new ArrayList<Double>();
ArrayList<Double> arrayList2 = new ArrayList<Double>();

for (int i =0;i<5;i++) 
{
    double randomValue = rangeMin + (rangeMax - rangeMin) * r.nextDouble();
    arrayList1.add(randomValue); 
}

for (int i =0;i<5;i++)  
{
    double randomValue = rangeMin + (rangeMax - rangeMin) * r.nextDouble();
    arrayList2.add(randomValue ); 
}


Double maxInArray1 = Collections.max(arrayList1);
Double maxInArray2 = Collections.max(arrayList2);

if (maxInArray1>maxInArray2)
{  
    System.out.println("first array have max");
}
else  if(maxInArray1<maxInArray2)
{
    System.out.println("second array have max");
}
else
{
    System.out.println("the max of second and first array is identical");

}
}
}

1 Answers1

1
import java.util.*;
public class TwoArrays
{
public static void main (String args [] )
{
Random r = new Random();
int rangeMin = 0;
int rangeMax = 50;

ArrayList<Double> arrayList1 = new ArrayList<Double>();
ArrayList<Double> arrayList2 = new ArrayList<Double>();

for (int i =0;i<5;i++) 
{
    double randomValue = rangeMin + (rangeMax - rangeMin) * r.nextDouble();
    arrayList1.add(randomValue); 
}

for (int i =0;i<5;i++)  
{
    double randomValue = rangeMin + (rangeMax - rangeMin) * r.nextDouble();
    arrayList2.add(randomValue ); 
}


Double maxInArray1 = Collections.max(arrayList1);
Double maxInArray2 = Collections.max(arrayList2);

if (maxInArray1>maxInArray2)
{  
    System.out.println("first array have max");
}
else  if(maxInArray1<maxInArray2)
{
    System.out.println("second array have max");
}
else
{
    System.out.println("the max of second and first array is identical");

}
}
}