Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Java by (9.5k points)

Here’s my code: 

Scanner input = new Scanner(System.in);

int array[] = new int[10];

System.out.println("Enter the numbers now.");

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

    int next = input.nextInt();

    // sentineil that will stop loop when 999 is entered

    if (next == 999) {

        break;

    }

    array[i] = next;

    // get biggest number

    getMaxValue(array);

    // get smallest number

    getMinValue(array);

}

System.out.println("These are the numbers you have entered.");

printArray(array);

// getting the maximum value

public static int getMaxValue(int[] array) {

    int maxValue = array[0];

    for (int i = 1; i < array.length; i++) {

        if (array[i] > maxValue) {

            maxValue = array[i];

        }

    }

    return maxValue;

}

// getting the miniumum value

public static int getMinValue(int[] array) {

    int minValue = array[0];

    for (int i = 1; i < array.length; i++) {

        if (array[i] < minValue) {

            minValue = array[i];

        }

    }

    return minValue;

}

//this method prints the elements in an array......

//if this case is true, then that's enough to prove to you that the user input has  //been stored in an array!!!!!!!

public static void printArray(int arr[]) {

    int n = arr.length;

    for (int i = 0; i < n; i++) {

        System.out.print(arr[i] + " ");

    }

}

Can anyone tell me how to print the maximum and minimum values in an array? 

1 Answer

0 votes
by (19.7k points)

Here’s the code you are using for method call: 

getMaxValue(array);

getMinValue(array);

Calling the methods itself, will not return the values. You need to use System.out.println. 

Try this code below: 

System.out.println(getMaxValue(array));

System.out.println(getMinValue(array)); 

Interested in Java? Check out this Java Certification by Intellipaat. 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 31, 2019 in Java by ParasSharma1 (19k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...