SlavCodes

Untitled

Apr 7th, 2020
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.math.BigDecimal;
  4. import java.util.*;
  5.  
  6. public class Main {
  7.  
  8. public static void main(String[] args) {
  9.  
  10. //Write a program that enters 3 real numbers
  11. // and prints them sorted in descending order. * Use nested if statements.
  12. Scanner userInput = new Scanner(System.in);
  13.  
  14. double a = Double.parseDouble(userInput.nextLine());
  15. double b = Double.parseDouble(userInput.nextLine());
  16. double c = Double.parseDouble(userInput.nextLine());
  17.  
  18. //a largest case
  19. if ((a > b) && (a > c)) {
  20. if (b > c) {
  21. System.out.printf("%s %s %s", checkDouble(a), checkDouble(b) ,checkDouble(c));
  22. } else if (c > b) {
  23. System.out.printf("%s %s %s", checkDouble(a), checkDouble(c), checkDouble(b));
  24. }
  25. //b largest case
  26. } else if ((b > a) && (b > c)) {
  27. if (a > c) {
  28. System.out.printf("%s %s %s", checkDouble(b), checkDouble(a), checkDouble(c));
  29. } else if (c > a) {
  30. System.out.printf("%s %s %s", checkDouble(b), checkDouble(c), checkDouble(a));
  31. }
  32. } else if ((c > a) && (c > b)) {
  33. //c is the largest
  34. if (a > b) {
  35. System.out.printf("%s %s %s", checkDouble(c), checkDouble(a), checkDouble(b));
  36. } else if (b > a) {
  37. System.out.printf("%s %s %s", checkDouble(c), checkDouble(b), checkDouble(a));
  38. }
  39. }
  40.  
  41. }
  42.  
  43.  
  44. public static String checkDouble(double n) {
  45. if ((int) Math.abs(n) < Math.abs(n)) {
  46. //return double as string
  47.  
  48. BigDecimal conveted = BigDecimal.valueOf(n).setScale(1,BigDecimal.ROUND_DOWN);
  49. return String.valueOf(conveted);
  50. }
  51. return String.valueOf((int) n);
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment