vladovip

Find Largest Three Values 1 - Java

Jan 15th, 2022
737
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.company;
  2.  
  3. import java.lang.invoke.SwitchPoint;
  4. import java.util.Scanner;
  5.  
  6. public class FindLargestThreeValues {
  7.     public static void main(String[] args) {
  8.  
  9.         Scanner input = new Scanner(System.in);
  10.         int countNumbersToFollow = Integer.parseInt(input.nextLine());
  11.         int num1 = Integer.MIN_VALUE;
  12.         int num2 = Integer.MIN_VALUE;
  13.         int num3 = Integer.MIN_VALUE;
  14.         int currentNumber = 0;
  15.  
  16.         for (int i = 1; i <= countNumbersToFollow; i++) {
  17.             currentNumber = input.nextInt();
  18.  
  19.             if (currentNumber > num1) {
  20.                 num3 = num2;
  21.                 num2 = num1;
  22.                 num1 = currentNumber;
  23.             } else if (currentNumber > num2) {
  24.                 num3 = num2;
  25.                 num2 = currentNumber;
  26.             } else if (currentNumber > num3) {
  27.                 num3 = currentNumber;
  28.             }
  29.         }
  30.         System.out.printf("%d, %d and %d", num1, num2, num3);
  31.  
  32.         //System.out.println(inputNumbers);
  33.  
  34.  
  35.     }
  36. }
  37.  
  38.  
  39.  
  40.  
  41. /*
  42. int max = Math.Max(num1, num2);
  43. max = Math.Max(max, num3);
  44.  
  45.         int largestInt = Math.max(num1, Math.max(num2, num3));
  46.  
  47.         OR second option:
  48.         double largestOfnum1num2 = num1 > num2 ? num1 : num2;
  49.         int largestInt = largestOfnum1num2 > num3 ? largestOfnum1num2 : num3;
  50.         System.out.println(number);
  51.  
  52.  ????? to find  three largest numbers in a sequence and prints them in descending order in the following format:
  53.             {largest}, {second_largest} and {third_largest}.
  54.  */
RAW Paste Data