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

Dúvida resolução ExercicioProposto01.java #31

Open
erickcred opened this issue Mar 27, 2023 · 1 comment
Open

Dúvida resolução ExercicioProposto01.java #31

erickcred opened this issue Mar 27, 2023 · 1 comment

Comments

@erickcred
Copy link

Boa tarde, tudo bom ?

Esse exercício resolvi ele dessa forma, gostaria de feedback sobre ele se esta correto se tem algum ponto para ser melhorado

Scanner leitor = new Scanner(System.in);
List temperaturas = new ArrayList<>();

    String[] messes = new String[] {
            "Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho"
    };

    for (int i = 0; i < messes.length; i++) {
        System.out.print("Informe a temperatura de " + messes[i] + ": ");
        temperaturas.add(leitor.nextDouble());
    }

    System.out.println("\nTodas as temperaturas: ");
    temperaturas.forEach(temp -> System.out.println(temp));

    System.out.println("\nCalculando media: ");
    Double media = temperaturas
            .stream()
            .reduce(0d, (total, element) -> total + element) / messes.length;
    System.out.println(media);

    System.out.println("\nMeses com as temperaturas acima da média: ");
    Iterator<Double> iterator = temperaturas.iterator();
    while (iterator.hasNext()) {
        Double temperatura = iterator.next();

        if (temperatura >= media) {
            System.out.println(messes[temperaturas.indexOf(temperatura)] + " - " + temperatura + "oC");
        }
    }
@cami-la
Copy link
Owner

cami-la commented Mar 29, 2023

Oi, @erickcred. Tudo certinho?
Sim! É uma forma válida de resolver o exercício também.

Há formas de simplificar ainda mais o exercício, mas de começo, está ótimo!

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