【问题标题】:Using Arrays to find Highest Number使用数组查找最大数
【发布时间】:2015-09-28 19:58:02
【问题描述】:

所以我想改变它以显示最大的数字而不是数字所在的列表。所以基本上我希望它找出那个数字的位置然后打印出那个数字是什么。

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");
    }
  }
}

【问题讨论】:

  • 您是在寻找最大的数字还是最大的数字在哪个列表中?
  • 我试图找出两个列表之间的最大数字是多少
  • 感谢 CollinD 的组织

标签: java arrays for-loop int


【解决方案1】:

尝试添加:

Double maxOfBoth = Math.max(maxInArray1, maxInArray2);

这将为您提供arrayList1arrayList2 之间的最大数字

【讨论】:

  • 在您创建 maxInArray2 变量之后直接
猜你喜欢
  • 1970-01-01
  • 2021-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-28
  • 1970-01-01
相关资源
最近更新 更多