Variables print often

Asked

Viewed 99 times

0

The variables media and contador, after the execution, they print many times and I don’t understand why. I wanted them to print only once at the end of the execution of the whole code.

My code:

import java.util.Scanner;

public class Chuva{
    public static void main(String[] args){

        int chuvadia;
        int [] chuva;
        int semanal;
        int quantidade;
        int y = 0;
        int i;
        double media;
        int soma = 0;
        int contador =0;
        double perc;


        Scanner input = new Scanner(System.in);

        //REGISTAR A QUANTIDADE DE CHUVA
        System.out.println("Insira a quantidade de chuva total esta semana");
        semanal = input.nextInt();

        //
        chuva = new int[7];

        if(semanal < 0){
            System.out.println("A quantidade de chuva nao pode ser negativa");
        } else {

        for(i = 0; i<chuva.length; i++){
                System.out.println("Indique a quantidade de chuva no dia" + y);
                y++;
                chuvadia = input.nextInt();

                if(chuvadia < 0){
                    System.out.println("A chuva nao pose ser negativa");
                    break;
                } else {

                chuva[i] = chuvadia;
                }
        }

        for(i = 0; i<chuva.length; i++){
            soma+= chuva[i];
        }
        media = (soma/7);
        System.out.println("A media e de:" + media);

        for(i=0;i<chuva.length;i++){
            double max = chuva[0];
            if( chuva[i] > max){
                max = chuva[i];
                System.out.println("O maior e:" + max);
            }

        }
        }
        for(i=0;i<chuva.length;i++){
            perc = (chuva[i]/semanal);
            if(perc < 0.2){
                contador++;
            }
            System.out.println("O n de dias com perc de chuva menor que 20% da semanal e:" + contador);
        }



}
}
  • 2

    Ué, the System.out.println is inside a for, there appears several times even

  • That’s it, thank you! Now I have another error, when running it is not giving the correct maximum here: for(i=0;i<rain.length;i++){ double max = rain[0]; if( rain[i] > max){ max = rain[i]; System.out.println("The biggest and:" + max); } Know why?

2 answers

0


The System.out.println is inside a for, there appears several times even.

For: @Sorack

0

Now I have another error, when executing is not giving the correct maximum here: for(i=0;i

max){ max = rain[i]; System.out.println("The largest e:" + max); } Knows why?

The problem is your variable max that is always picking up the first position of the rain array.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.