SlavCodes

Find Largest Three Values

Jul 14th, 2019
453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Test3 {
  3. public static void main(String[] args) {
  4. Scanner reader = new Scanner(System.in);
  5. int max1 = Integer.MIN_VALUE + 3;
  6. int max2 = Integer.MIN_VALUE + 2;
  7. int max3 = Integer.MIN_VALUE + 1;
  8.  
  9. int stopNum = Integer.parseInt(reader.nextLine());
  10. int input;
  11.  
  12. for (int i = 1; i <= stopNum ; i++) {
  13. input = Integer.parseInt(reader.nextLine());
  14.  
  15. if (input > max1) {
  16. max3 = max2;
  17. max2 = max1;
  18. max1 = input;
  19. }
  20.  
  21. if (input < max1 && input > max3) {
  22. if(input > max2) {
  23. max3 = max2;
  24. max2 = input;
  25. }
  26. }
  27.  
  28. if (input < max1 && input < max2) {
  29. if (input > max3) {
  30. max3 = input;
  31. }
  32. }
  33. }
  34. System.out.println(max1 + ", " + max2 + " and " + max3);
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment