Advertisement
veronikaaa86

09. Greater of Two Values

Oct 5th, 2022
1,087
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.63 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.                 break;
  18.             case "char":
  19.                 char firstSymbol = scanner.nextLine().charAt(0);
  20.                 char secondSymbol = scanner.nextLine().charAt(0);
  21.  
  22.                 System.out.println(getMax(firstSymbol, secondSymbol));
  23.                 break;
  24.             case "string":
  25.                 String firstText = scanner.nextLine();
  26.                 String secondText = scanner.nextLine();
  27.  
  28.                 System.out.println(getMax(firstText, secondText));
  29.                 break;
  30.         }
  31.     }
  32.  
  33.     public static int getMax(int firstNum, int secondNum){
  34.         if (firstNum > secondNum) {
  35.             return firstNum;
  36.         } else {
  37.             return secondNum;
  38.         }
  39.     }
  40.  
  41.     public static char getMax(char firstSymbol, char secondSymbol){
  42.         if (firstSymbol > secondSymbol) {
  43.             return firstSymbol;
  44.         } else {
  45.             return secondSymbol;
  46.         }
  47.     }
  48.  
  49.     public static String getMax(String firstText, String secondText) {
  50.         if (firstText.compareTo(secondText) > 0) {
  51.             return firstText;
  52.         } else {
  53.             return secondText;
  54.         }
  55.     }
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement