首页 > 解决方案 > 基本Java数组编码,查找数组中的最大数

问题描述

编写一个数组的代码,然后对其进行排序并返回最大的数 编写一个程序,其中 main 方法创建一个具有 10 个 int 类型槽的数组。为每个插槽分配一个随机生成的整数。调用一个函数,将数组传递给它。调用
你的主要方法。您的 main 方法应显示返回的数字。使用 Random 对象生成整数。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];
    }

}

标签: javaarrays

解决方案


推荐阅读