有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

这是我在学校问的一个编码问题,我不想说“嘿,伙计们,帮我做作业!”,我真的想知道这里发生了什么。我们刚开始使用数组,它们让我有些困惑,所以我正在寻求帮助。 下面是完整的问题:

Write a program in which the main method creates an array with 10 slots of type int. Assign to each slot a randomly-generated integer. Call a function, passing it the array. The called function should RETURN the largest integer in the array to your main method. Your main method should display the number returned. Use a Random object to generate integers. Create it with

Random r = new Random(7);

Generate a random integer with

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并返回它

非常感谢您的帮助,因为我非常想了解这里发生了什么。谢谢大家!


共 (6) 个答案

  1. # 1 楼答案

    这可以在一个简单的for循环中完成,无需有两个循环

    public static void main(String[] args) {
            Integer[] randomArray = new Integer[10];
            randomArray[0] = (int)(Math.random()*100);
            int largestNum = randomArray[0];
            for(int i=1; i<10 ;i++){
                randomArray[i] = (int)(Math.random()*100);
                if(randomArray[i]>largestNum){
                    largestNum = randomArray[i];
                }
            }
            System.out.println(Arrays.asList(randomArray));
            System.out.println("Largest Number :: "+largestNum);
        }
    
  2. # 2 楼答案

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

  3. # 3 楼答案

    到目前为止,您所得到的几乎是正确的,但是您当前在for循环的每个迭代中使用相同的随机数。即使您需要为for循环的每次迭代获得一个新的随机数。这是因为随机对象是如何定义的。您可以通过以下方式更改代码来实现这一点:

    import java.util.Random;
    public class Q1 {
    
        public static void main(String[] args) { 
        Random r = new Random(7);
        int[] count = new int[11];
        for (int i = 0; i < count.length; i++)
        {
            int x = r.nextInt(); // You need to generate a new random variable each time
            count[i] = x;
        }
    }
    

    请注意,这段代码不是最优的,但它是对已有代码的最小更改

    要从数组中获取最大值,需要编写另一个for循环,然后将数组中的每个值与迄今为止的最大值进行比较。您可以通过以下方式执行此操作:

    int largest = 0; // Assuming all values in the array are positive.
    for (int i = 0; i < count.length; i++)
            {
                if(largest < count[i]) { // Compare whether the current value is larger than the largest value so far
                   largest = count[i]; // The current value is larger than any value we have seen so far, 
                                      // we therefore set our largest variable to the largest value in the array (that we currently know of)
                   }
            }
    

    当然,这也不是最优的,这两件事可以在同一个for循环中完成。但这应该更容易理解

  4. # 4 楼答案

    可以通过对数组使用一个简单的for循环来实现。 首先,你必须创建一个独立的int变量(例如:int a)并赋值0,在循环的每次迭代中,你必须将数组项与变量a进行比较

    a < count[i]
    

    如果这是真的,你必须把计数[i]值赋给变量a。这个循环将持续到数组的最后一个索引,你将在avariabe中拥有最大的数字。因此,简单地SYSOUTa变量

    重要:我没有在这里发布代码,因为我想让你理解这个概念,因为如果你理解了它,那么你将来可以自己解决这些问题
    希望这有帮助

  5. # 5 楼答案

    你的代码应该是这样的。阅读评论以理解它

    public class Assignment {
    
    
    
        public static int findMax(int[] arr) {       // Defiine a function to find the largest integer in the array
            int max = arr[0];          // Assume first element is the largest element in the array
            for (int counter = 1; counter < arr.length; counter++)  // Iterate through the array 
            {
                 if (arr[counter] > max)  // if element is larger than my previous found max
                 {
                  max = arr[counter]; // then save the element as max
                 }
            }
            return max;  // return the maximum value at the end of the array
        }
    
    
    
    
        public static void main(String[] args) { 
    
        int numberofslots =10;
    
        int[] myIntArray = new int[numberofslots];  // creates an array with 10 slots of type int
    
        Random r = new Random(7);
    
        for (int i = 0; i < myIntArray.length; i++)  // Iterate through the array 10 times
        {
    
         int x = r.nextInt();
         myIntArray[i] = x;  // Generate random number and add it as the i th element of the array.
        }
    
        int result =  findMax(myIntArray); // calling the function for finding the largest value 
        System.out.println(result); // display the largest value
    
        }
    }
    

    希望你能通过阅读评论来理解代码

  6. # 6 楼答案

    Here is how to generate Random ints

    public static void main(String[] args) {
         int []count = new int[10];
              Random r = new Random(7); 
              int x=0;
            for (int i = 0; i < count.length; i++)
            {
                x = r.nextInt();
                count[i] = x;
    
            }
           System.out.println("Max Number :"+maxNumber(count));}//Getting Max Number
    

    Here is how to make method and get max number from list.

    static int maxNumber(int[] mArray){//Passing int array as parameter
            int max=mArray[0];
            for(int i=0;i<mArray.length;i++){
                if(max<mArray[i]){//Calculating max Number
                    max=mArray[i];
                }
            }
    
            return max;//Return Max Number.
        }
    

    询问是否有什么不清楚的地方。 这就是我们如何制作返回int的方法。