Advertisement
veronikaaa86

09. Greater of Two Values

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