找到最大的随机数组 [英] finding max of random array

查看:142
本文介绍了找到最大的随机数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试一下,找出如何找到一个随机数数组的最大值。这是我正在调试的代码:到目前为止,我得到的是数组中的一个值,但它不是最大值。

I have been trying for a while to figure out how to find the max value of an array of random numbers. Here is the code i am debugging: as of now all I am getting is one value from the array but it isnt the max value.

public static void main(String[] args) {
        System.out.println(intOfMaxInRange(randomIntArray(10), 1,30));
    }
     public static int random(int low, int high){
        int x=(int)(Math.random()*high+low);
        return x;
    }
    public static int[] randomArray(int n){
        int[] a = new int[n];
        for (int i = 0; i<a.length; i++) {
        a[i] = randomInt (1,30);
    }
        return a;
    }
    public static int intOfMax( int[] array){
        int max=array[0];
        for(int i=1;i<array.length;i++){
            if (array[i] > max) {
        }
        }
        return max;

    }


推荐答案

在正确的方式,只需添加 max = array [i]; intOfMax()方法:

You are on the right way, just add max = array[i]; into intOfMax() method:

 for(int i=1;i<array.length;i++)
 {
    if (array[i] > max)
       {
          max = array[i];
       }
 }

这篇关于找到最大的随机数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆