I have a confusion of how to put that max and min in main method so it would run.
PS: there might be mistakes

The goal is to create a method that enter any numbers by user and it suppose to show the Max and Min.

PLEASE HELP~! what is wrong with this?

public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("How many numbers will be entered?");
int num = scanner.nextInt();
double[] grades = new double[num];

    for(int i = 0; i < grades.length; i++)
    {
        System.out.println("Enter an integer: ");
        grades[i] = scanner.nextDouble();
    }



}

public static int max(int[] numbers)
{

    int max = numbers[0];
    for(int i = 0; i < numbers.length; i++)
    {
        if(numbers[i] > max)
            max = numbers[i];
        System.out.println("Maximum = " + max);

    }

    return max;



}

public static int min(int[] numbers)
{

    int min = numbers[0];
    for(int i = 0; i < numbers.length; i++)
    {
        if(numbers[i] < min)
            min = numbers[i];
        System.out.println("Minimum = " + min);

    }
    return min;


}

Edited 4 Years Ago by Masque: fixing mistakes

what is wrong with this?

Can you post the console from when the code is executed and add some coments to it that explains what the problem is?

oh, ok
The problem is, when i run it, the max and min didn't show up, i think it's because i didn't put it in main method?

here's the console:

How many numbers will be entered?
3
Enter an integer:
50
Enter an integer:
60
Enter an integer:
70

That's it, soemthing's wrong with my MAX n MIN >.<
PLEASE HELP!~~~

Where do you call the methods you are trying to test? You need to call them if you want them to execute.

I mean do i have to put that int max into main so JAVA can execute it?

the question is:
i dont know how to execute the method like: int max(int[] numbers) that when something inside the parameter after i return it, idk how to display it for the output.

You need to create an array of int to be able to call the methods. Your code has a double array which will not work. Both methods require int arrays for arguments.

You need to read the tutorial about how to use methods. Click the link in my earlier post.

Edited 4 Years Ago by NormR1

you didn't call the methods min and max, and while you are creating an array of doubles, your methods expect an array of ints as parameters.

This article has been dead for over six months. Start a new discussion instead.