package com.codegym.task.task04.task0419;
/*
Maximum of four numbers
*/
import java.io.*;
import java.util.Arrays;
public class Solution {
public static void main(String[] args) throws Exception {
//write your code here
BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
int w = Integer.parseInt(reader.readLine());
int x = Integer.parseInt(reader.readLine());
int y = Integer.parseInt(reader.readLine());
int z = Integer.parseInt(reader.readLine());
int[] arr = { w , x , y , z};
Arrays.sort(arr);
int max = 0;
for(int i = 0; i < arr.length - 1 ; i++){
if(arr[i] < arr[i+1]){
max = arr[i+1];
}
}
System.out.println(max);
}
}

# Not sure why this doesnt meet the third Condition

Under discussion

Comments (4)

- Popular
- New
- Old

You must be signed in to leave a comment

Shomir Uddin

24 September 2022, 16:22

When verifying:
3. The program should display the maximum of four numbers.
the above condition isnt met.

0

Thomas

24 September 2022, 18:24useful

The code does not give the correct result for all cases. Try 1 1 1 1
Expected result 1, your codes output 0

+1

Shomir Uddin

25 September 2022, 04:45

Ok thats why <= solved the issue. Thanks!

+1

Thomas

25 September 2022, 08:32

Yep, that would do the trick. The loop however is not necessary at all. You do not need to compare each element to the next to check if the prev one is smaller then the following. You know that it is exactly that way. That's what sorting means. First element is the smallest, the last element is the largest. So just accessing arr[arr.length - 1] would give you the max as well.
But sorting isn't necessary in the first place to just get the min or max. You read the first number and then you just compare to the following three you read from the console.

Or you use a max method, your own written or the one from the Math lib like
or... there are lot more ways to do that 0