Advertisement
veronikaaa86

09. Greater of Two Values

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