Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Desafio 1: NumeroReverso #4

Open
paulofranklins2 opened this issue May 25, 2022 · 2 comments
Open

Desafio 1: NumeroReverso #4

paulofranklins2 opened this issue May 25, 2022 · 2 comments

Comments

@paulofranklins2
Copy link

paulofranklins2 commented May 25, 2022

Pode não ser a maneira mais apropriada, mas decidi compartilhar para que não de a impressão de que a única forma de reverter o número seja transformando ele em String.
Como disse, pode não ser a maneira mais adequada, porem interessante buscar outras formas somente para testes e aprendizado.

import java.util.Scanner;

import static java.lang.String.copyValueOf;
import static java.lang.String.valueOf;


// #1
public class ReverseNumberNoConvert {
    public static void reverseNumber() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Input must have 4 digits, between 1000 and 9999.");
        int input = scanner.nextInt();
        while (input < 1000 || input > 9999) {
            System.out.println("Input must have 4 digits, between 1000 and 9999.");
            input = scanner.nextInt();
        }
        String savingReverseNumber = "";
        String convertToString = valueOf(input);
        System.out.println("Input: " + convertToString);
        char reverseNum;
        for (int i = 0; i < convertToString.length(); i++) {
            reverseNum = convertToString.charAt(i);
            savingReverseNumber = reverseNum + savingReverseNumber;
        }
        System.out.println("Output: " + savingReverseNumber);
    }
}

// #2
class ReverseNumberNoStringBuilder {
    public static void reverseNumber() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Input must have 4 digits, between 1000 and 9999.");
        int reverseNum = 0;
        String savingNumb = "";
        int input = scanner.nextInt();
        while (input < 1000 || input > 9999) {
            System.out.println("Input must have 4 digits, between 1000 and 9999.");
            input = scanner.nextInt();
        }
        System.out.println("Input: " + input);
        for (; input != 0; ) {
            reverseNum *= 10;
            savingNumb += input % 10;
            input = input / 10;
        }
        System.out.println("Output: " + savingNumb);
    }
}

// #3
class ReverseNumber {
    public static void reverseNumber() {
        Scanner scanner = new Scanner(System.in);
        do {
            System.out.println("Input must have 4 digits, between 1000 and 9999.");
            int input = scanner.nextInt();
            String inputConvertToString = valueOf(input);
            if (input >= 1000 && input <= 9999) {
                StringBuilder stringBuilder = new StringBuilder(inputConvertToString);
                System.out.println("input: " + input);
                System.out.println("output: " + stringBuilder.reverse());
                break;
            }
        } while (true);
    }
}

class ReverseNumberMain {
    public static void main(String[] args) {
        ReverseNumberNoConvert.reverseNumber();
        ReverseNumberNoStringBuilder.reverseNumber();
        ReverseNumber.reverseNumber();
    }
}
@cami-la
Copy link
Owner

cami-la commented May 25, 2022

Ótimo amigo! Muito obrigada pelo interesse em compartilhar seu conhecimento. <3

Quando eu falei que "Você não consegue pegar um numeral e pegar o inverso dele" estava me referindo que a classe Integer não tem um método que faz essa função.

Logo, podemos transformar esse Integer ou o tipo primitivo int em uma StringBuilder e utiliza o método reverse() que faz isso de uma forma relativamete simples. (:

Ah, outro colega também compartilhou uma forma de fazer utilizando resto da divisão:

while(numero != 0)
    {
        restante = numero % 10;
        reverso = reverso * 10 + restante;
        numero = numero/10;
    }

    System.out.println(reverso);

De fato, para um problema, exitem n maneiras de interpretar e resolver. Cabe a nós como bons programadores, encontrarmos a maneira de resolver que performa melhor! (:

Bons estudos e sigo à disposição.

@paulofranklins2
Copy link
Author

Obrigado Camila <3
Como você mesma disse reverter um integer não dá, porem ha N formas de fazer.
Como disse eu só quis compartilhar outras formas de fazer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants