使用数组查找2个列表之间的最大数字

时间:2015-09-28 18:12:02

标签: java arrays for-loop random double

创建一个程序,从1到50随机生成5个数字,打印出来然后再用第二个数组做同样的事情。有程序确定哪个是2 ????之间的最大数字 好的,所以这是我问自己的一个问题,这可能完全错了,但我试过了: 所以这不是我想要的,但它很接近。我希望它显示实际的最高数字。

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 个答案:

答案 0 :(得分: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");

}
}
}