Didart

Greater Of Two Values

Oct 7th, 2021
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.52 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class GreaterOfTwoValues {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         String type = scanner.nextLine();
  8.  
  9.         switch (type) {
  10.             case "int":
  11.                 int firstNum = Integer.parseInt(scanner.nextLine());
  12.                 int secondNum = Integer.parseInt(scanner.nextLine());
  13.                 System.out.println(getMax(firstNum, secondNum));
  14.                 break;
  15.             case "char":
  16.                 char firstChar = scanner.nextLine().charAt(0);
  17.                 char secondChar = scanner.nextLine().charAt(0);
  18.                 System.out.println(getMax(firstChar, secondChar));
  19.                 break;
  20.             case "string":
  21.                 String firstText = scanner.nextLine();
  22.                 String secondText = scanner.nextLine();
  23.                 System.out.println(getMax(firstText, secondText));
  24.                 break;
  25.         }
  26.     }
  27.  
  28.     public static int getMax(int firstNum, int secondNum) {
  29.         if (firstNum > secondNum) {
  30.             return firstNum;
  31.         }
  32.         return secondNum;
  33.     }
  34.  
  35.     public static char getMax(char firstChar, char secondChar) {
  36.         if (firstChar > secondChar) {
  37.             return firstChar;
  38.         }
  39.         return secondChar;
  40.     }
  41.  
  42.     public static String getMax(String firstText, String secondText) {
  43.         if (firstText.compareTo(secondText) >= 0) {
  44.             return firstText;
  45.         }
  46.         return secondText;
  47.     }
  48. }
  49.  
  50.  
Advertisement
Add Comment
Please, Sign In to add comment