简体   繁体   中英

return the maximum value in the array?

I had an an assignment with this question

Write a function maxArray which receives an array of double's and returns the maximum value in the array. using this function

double maxArray(double dar[], int size);

I did what he want and I had problem with the calling sentence within the main method !!

here is my code :

public class Q3 {

    public static void main(String[] args) {
        double dar[] = { 22.5 , 10.23 , 15.04 , 20.77 };
        double max = maxArray(dar,4);
        System.out.println("the largest number is : " + max);
    }

    double maxArray(double dar[], int size) {
        double maxV = 0;
        for (int i = 0; i < dar.length; i++) {
            if (dar[i] > maxV ) {         
                maxV = dar[i];          
            }
        }
        return maxV;
    } 
}

The reason you can't call your method from main() is that main() is static whereas your method isn't.

Change it to:

static double maxArray(double dar[], int size)

While you're at it, remove size since it's not necessary.

It is probably also worth noting that your method would fail if the array were to contain negative numbers.

your maxArray method is a non static method. you cannot access non-static methods from static methods without an instance of the class, you should create an instance of your class and call maxArray method

double max = new Q3().maxArray(dar,4);

Or alternatively, you could always mark your maxArray method static and call it directly from main method.

Declare your maxArray as static , so you can access it as from a static method main()

or

You create an instance of your class and call it from the object.

your issue is you are trying to call maxArray, a non-static method, from your main method, which is static. That's a problem because a non-static method can only be called from an instance of the class, whereas a static method is called via the class itself.

Either make your maxArray a static method, or initialize a Q3 object in your main method, and call maxArray like that.

Your method has to be static, so you have to say

static double maxArray(double dar[], int size)

Here are some hints how you could improve your method: since you don't use the value "size" once, you can either throw it out or replace the i < dar.length with i < size .

Also, when initializing maxV in the maxArray method, you might want to use the first value of the array ( double maxV = dar[0] ), because if all doubles in the array are negative, maxV with the number 0 will be the highest. (You could also use the lowest double value possible by saying double maxV = Double.MIN_NORMAL ).

1) make your method static
2) Remember in java use BigDecimal class to do any decimal arithmetic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM