Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.math.BigDecimal;
- import java.util.*;
- public class Main {
- public static void main(String[] args) {
- //Write a program that enters 3 real numbers
- // and prints them sorted in descending order. * Use nested if statements.
- Scanner userInput = new Scanner(System.in);
- double a = Double.parseDouble(userInput.nextLine());
- double b = Double.parseDouble(userInput.nextLine());
- double c = Double.parseDouble(userInput.nextLine());
- //a largest case
- if ((a > b) && (a > c)) {
- if (b > c) {
- System.out.printf("%s %s %s", checkDouble(a), checkDouble(b) ,checkDouble(c));
- } else if (c > b) {
- System.out.printf("%s %s %s", checkDouble(a), checkDouble(c), checkDouble(b));
- }
- //b largest case
- } else if ((b > a) && (b > c)) {
- if (a > c) {
- System.out.printf("%s %s %s", checkDouble(b), checkDouble(a), checkDouble(c));
- } else if (c > a) {
- System.out.printf("%s %s %s", checkDouble(b), checkDouble(c), checkDouble(a));
- }
- } else if ((c > a) && (c > b)) {
- //c is the largest
- if (a > b) {
- System.out.printf("%s %s %s", checkDouble(c), checkDouble(a), checkDouble(b));
- } else if (b > a) {
- System.out.printf("%s %s %s", checkDouble(c), checkDouble(b), checkDouble(a));
- }
- }
- }
- public static String checkDouble(double n) {
- if ((int) Math.abs(n) < Math.abs(n)) {
- //return double as string
- BigDecimal conveted = BigDecimal.valueOf(n).setScale(1,BigDecimal.ROUND_DOWN);
- return String.valueOf(conveted);
- }
- return String.valueOf((int) n);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment