Ad
Code
Diff
  • import java.util.*;
    import java.util.stream.Collectors;
    
    public class MaxNumber {
        public static long print(long number) {
          return Long.parseLong(
            String.valueOf(number)
                .chars()
                .mapToObj(ch -> String.valueOf(Character.getNumericValue(ch)))
                .sorted(Comparator.reverseOrder())
                .collect(Collectors.joining())
        );
      }
    }
    • import java.util.Arrays;
    • import java.util.Scanner;
    • import java.util.*;
    • import java.util.stream.Collectors;
    • public class MaxNumber {
    • public static long print(long number) {
    • String numeroString = Long.toString(number);
    • char[] digitos = numeroString.toCharArray();
    • Arrays.sort(digitos);
    • StringBuilder numeroOrdenadoStr = new StringBuilder(new String(digitos));
    • numeroOrdenadoStr.reverse();
    • return Long.parseLong(numeroOrdenadoStr.toString());
    • }
    • public static void main(String[] args) {
    • Scanner scanner = new Scanner(System.in);
    • System.out.print("Ingrese un numero: ");
    • long number = scanner.nextLong();
    • long maxNumber = print(number);
    • System.out.println("El número más alto que se puede formar con estos dígitos es: " + maxNumber);
    • return Long.parseLong(
    • String.valueOf(number)
    • .chars()
    • .mapToObj(ch -> String.valueOf(Character.getNumericValue(ch)))
    • .sorted(Comparator.reverseOrder())
    • .collect(Collectors.joining())
    • );
    • }
    • }