如何返回包含 10 个随机整数的数组中的最大整数?

所以这是我在学校遇到的一个编码问题,我不想说“嘿伙计们为我做作业!”,我实际上想了解这里发生了什么。我们刚刚开始使用数组,它们让我感到困惑,所以我正在寻找一些帮助。这是完整的问题:

编写一个程序,其中 main 方法创建一个具有 10 个 int 类型槽的数组。为每个插槽分配一个随机生成的整数。调用一个函数,将数组传递给它。被调用的函数应该将数组中最大的整数返回给您的主方法。您的主要方法应该显示返回的数字。使用 Random 对象生成整数。创建它

Random r = new Random(7);

生成一个随机整数

x = r.nextInt();

所以,这是我到目前为止所拥有的:

import java.util.Random;

public class Q1 {


    public static void main(String[] args) { 

    Random r = new Random(7);

    int[] count = new int[11];

    int x = r.nextInt();

    for (int i = 0; i < count.length; i++)

    {

        count[i] = x;

    }

}

我用 10int秒创建了该数组,然后使用for循环来分配随机生成整数的每个插槽。


不过,我很难确定接下来要做什么。我不确定要创建什么样的方法/函数,然后如何从那里获得最大的int并返回它。


任何帮助都非常感谢,因为我真的很想了解这里发生了什么。谢谢!


泛舟湖上清波郎朗
浏览 146回答 3
3回答

小怪兽爱吃肉

这是生成随机整数的方法public static void main(String[] args) {&nbsp; &nbsp; &nbsp;int []count = new int[10];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Random r = new Random(7);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int x=0;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < count.length; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x = r.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count[i] = x;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Max Number :"+maxNumber(count));}//Getting Max Number以下是如何制作方法并从列表中获取最大数量。static int maxNumber(int[] mArray){//Passing int array as parameter&nbsp; &nbsp; &nbsp; &nbsp; int max=mArray[0];&nbsp; &nbsp; &nbsp; &nbsp; for(int i=0;i<mArray.length;i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(max<mArray[i]){//Calculating max Number&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max=mArray[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return max;//Return Max Number.&nbsp; &nbsp; }问有没有什么不清楚的。这就是我们如何制作返回 int 的方法。

米脂

将最大值初始化为数组的第一个值。然后使用 for 循环迭代数组并使用最大值检查数组当前值。或者你可以sort数组并返回。祝你好运!

BIG阳

你的代码应该是这样的。阅读评论以了解它public class Assignment {&nbsp; &nbsp; public static int findMax(int[] arr) {&nbsp; &nbsp; &nbsp; &nbsp;// Defiine a function to find the largest integer in the array&nbsp; &nbsp; &nbsp; &nbsp; int max = arr[0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Assume first element is the largest element in the array&nbsp; &nbsp; &nbsp; &nbsp; for (int counter = 1; counter < arr.length; counter++)&nbsp; // Iterate through the array&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (arr[counter] > max)&nbsp; // if element is larger than my previous found max&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max = arr[counter]; // then save the element as max&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return max;&nbsp; // return the maximum value at the end of the array&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) {&nbsp;&nbsp; &nbsp; int numberofslots =10;&nbsp; &nbsp; int[] myIntArray = new int[numberofslots];&nbsp; // creates an array with 10 slots of type int&nbsp; &nbsp; Random r = new Random(7);&nbsp; &nbsp; for (int i = 0; i < myIntArray.length; i++)&nbsp; // Iterate through the array 10 times&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp;int x = r.nextInt();&nbsp; &nbsp; &nbsp;myIntArray[i] = x;&nbsp; // Generate random number and add it as the i th element of the array.&nbsp; &nbsp; }&nbsp; &nbsp; int result =&nbsp; findMax(myIntArray); // calling the function for finding the largest value&nbsp;&nbsp; &nbsp; System.out.println(result); // display the largest value&nbsp; &nbsp; }}希望你能通过阅读评论来理解代码..
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java