Skip to content
Advertisement

How do I select the max value from an array?

I’m creating a elementary math tutorial, and for this portion I have an array, the array is filled with 5 random numbers, between 0 and 9. The question I am creating would give 5 random digits, and say “What is the highest number that you could make with these digits?”, and then I store that number in a variable

    // These are all Random numbers using the Random method
    a = num.nextInt(9);
    b = num.nextInt(9);
    c = num.nextInt(9);
    d = num.nextInt(9);
    e = num.nextInt(9);
    f = num.nextInt(9);

    // Asks the question right here (not important right now)
    // prints out 5 digits here (again, not important right now)

    g = ans.nextInt(); // this is the users response 

    int h3[] = {a, b, c, d, e, f}; // this array holds the random numbers

Advertisement

Answer

    Arrays.sort(h3);
    int maxNo = h3[h3.length-1].

This should do what you require. Your highest value will be set to maxNo. Dont forget to import Java.util.*;

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement