Advertisement
AngelKejov

Greater of two values

Jun 11th, 2021
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.40 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class GreaterOfTwoValues {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner sc = new Scanner(System.in);
  9.  
  10.         String typeVariable = sc.nextLine();
  11.         if (typeVariable.equals("int")) {
  12.             int n1 = Integer.parseInt(sc.nextLine());
  13.             int n2 = Integer.parseInt(sc.nextLine());
  14.             System.out.println(getMax(n1, n2));
  15.         } else if (typeVariable.equals("char")) {
  16.             char ch1 = sc.nextLine().charAt(0);
  17.             char ch2 = sc.nextLine().charAt(0);
  18.             System.out.println(getMax(ch1, ch2));
  19.         } else if (typeVariable.equals("string")) {
  20.             String str1 = sc.nextLine();
  21.             String str2 = sc.nextLine();
  22.             System.out.println(getMax(str1, str2));
  23.         }
  24.     }
  25.  
  26.     private static int getMax(int n1, int n2) {
  27.         int res = 0;
  28.         res = Math.max(n1, n2);
  29.  
  30.         return res;
  31.     }
  32.  
  33.     private static char getMax(char ch1, char ch2) {
  34.         char res = ' ';
  35.         if (ch1 > ch2) {
  36.             res = ch1;
  37.         } else {
  38.             res = ch2;
  39.         }
  40.  
  41.         return res;
  42.     }
  43.  
  44.     private static String getMax(String str1, String str2) {
  45.         String res = "";
  46.         if (str1.length() > str2.length()) {
  47.             res = str1;
  48.         } else {
  49.             res = str2;
  50.         }
  51.  
  52.         return res;
  53.     }
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement