基本Java数组编码,在数组中找到最大的数字

问题描述 投票:0回答:1

创建数组的代码,然后对其进行排序并返回最大数编写一个程序,其中main方法使用以下方法创建一个数组int类型的10个插槽。为每个插槽分配一个随机生成的整数。调用一个函数,将其传递给数组。被叫您的主要方法。您的主要方法应显示返回的数字。使用随机对象生成整数。 Createwith

    Random r = new Random(7);

使用生成随机整数

    x = r.nextInt();

我到目前为止编写的代码

import java.util.*;

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

        int list[] = new int[10];


        System.out.println(returnLargest(list));
    }

    public static int returnLargest(int a[])
    {
        Random r = new Random(7);
        for (int i=0; i <a.length; i++)
            {
                a[i] = r.nextInt();
            }
        Arrays.sort(a);
        return a[9];
    }

}
java arrays
1个回答
0
投票

如果您想获得最大的数字,您可以使用Arrays.sort(list)现在您的数组已排序。要获得最高的数字,只需选择数组的最后一个元素。希望我能为您服务。

© www.soinside.com 2019 - 2024. All rights reserved.