Advertisement
veronikaaa86

09. Greater of Two Values

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