Okay i need this quick please i am kind of stuck basically i have got to find the highest and lowest numbers out of four integers.

Is there a function in java.math or do i need to make one

using a loop maybe?

Well, if they are four individual variables you could use nested min/max calls

int a = 2;
int b = 22;
int c = 3;
int d = 345;
System.out.println(String.valueOf(Math.min(Math.min(a, b), Math.min(c, d))));
System.out.println(String.valueOf(Math.max(Math.max(a, b), Math.max(c, d))));

If they are an array, you could just call Arrays.sort() and pull the first and last, or loop them and find them yourself.

Unless you're doing it in a tight loop, it probably won't make much difference for just 4 values.

Edit: Actually, I wouldn't bother sorting if they are an array. It's just unnecessary overhead. Just loop them

int[] arr = {2,22,3,345};
int min=Integer.MAX_VALUE;
int max=Integer.MIN_VALUE;
for (int i : arr){
    if (i<min) min = i;
    if (i>max) max = i;
}
System.out.println("min: "+min);
System.out.println("max: "+max);
Comments
helpful

we havent done arrays yet

i have some pseudocode using nested IFs

actually its not 4 values, i just checked the brief and it says they want the user to be able to keep inserting values until -1 is given (exit code)

i think maybe (pseudocode)

tell user to enter some numbers to find the highest and lowest and enter -1 when done.
               
initialise all values to zero but not the smallest and biggest because you dont want to compare to zero?
         
get first number, store away in the variable current
set it to be the initial value for biggest and smallest
if -1 is entered then exit
            
else enter DO loop

in loop get input. Check it -1, else check if  input > biggest or < smallest , if so then make it the new value for it.

loop while value is not -1?

Then basically like the array loop I posted, but you just check against the input and change the values if needed

int min=Integer.MAX_VALUE;
int max=Integer.MIN_VALUE;
while ( [I]userInput()[/I] ){
    if (i<min) min = i;
    if (i>max) max = i;
}

Of course, that still leaves it to you to get the input value and check against -1 as you loop.

yeah thanks for your help + 1 rep :)

yeah i thaught about using arrays but we havent covered them yet whereas we had just finished selection and iteration

best to do it the way they want

solved.

This question has already been answered. Start a new discussion instead.