Print variable within a repeat structure

4

You are not recognizing the variable soma because it is inside the loop . What do I do to fix this?

public static void main(String[] args) {

    int tipo;

    int restante = 0;
    int bim = 4;
    //double soma= 0;

    double[] nota = new double[bim];

    Scanner x = new Scanner(System.in);

    System.out.println("inisira a quatidade de bimestres");
    bim = x.nextInt();

    for (int i = 0; i < bim; i++) {
        System.out.println("Insira a nota do " + (i + 1) + "º Bimestre");
        nota[i] = x.nextDouble();
        double soma = 0;
        soma = soma + nota[i];

    }

    double media = soma / bim;
    System.out.println(media);
}
    
asked by anonymous 29.08.2018 / 15:47

4 answers

5

Get used to declaring the variable where it will be needed. And understand what's happening with your code. If you do a table test you will see that it is already wrong because every step of the loop the variable will be reset and this is not what you want, it makes no sense to use it like that, and you had started doing it right, then spoiled :(. do not use variables with no need See how simple it is:

import java.util.*;

class Ideone {
    public static void main(String[] args) {
        Scanner x = new Scanner(System.in);
        System.out.println("inisira a quatidade de bimestres");
        int bim = x.nextInt();
        double soma = 0;
        for (int i = 0; i < bim; i++) {
            System.out.println("Insira a nota do " + (i + 1) + "º Bimestre");
            soma += x.nextDouble();
        }
        System.out.println(soma / bim);
    }
}

See running ideone . And no Coding Ground . Also I put in GitHub for future reference .

    
29.08.2018 / 16:01
4

An important concept in programming is called scope. In Java, it is defined by curly braces , { } , but we will call them block, ie each pair of { } is a block and, as you may have noticed, you can have a block within another. If you did not notice, notice this typical structure of a Java class:

class Pessoa {

   public void chamarPeloNome(String nome) {

      if(nome != null) {
         System.out.println("Olá, " + nome);
      }
   }
}

See that the class has a block, the method has a block and the if inside the method has a block, the if of the block is the innermost and the one of the outermost class.

That said, the general rule in Java says that variables defined within a block are only visible within that block or in blocks that are internal to it.

Look at your code:

(...)
for (int i = 0; i < bim; i++) {
        System.out.println("Insira a nota do " + (i + 1) + "º Bimestre");
        nota[i] = x.nextDouble();
        double soma = 0;
        soma = soma + nota[i];

    }

    double media = soma / bim;

The variable soma is defined in a block, but you are trying to access it in a block outside, and this is impossible in Java, because, as we said, a variable is accessible in the same block in which it is declared or in internal blocks to it, never in external blocks.

How to solve it then? Simple. Just declare this variable out of for . As it stands today, it is only visible inside and any other block within the for that you eventually create (a if , for example).

    
29.08.2018 / 16:10
0

Place the sum variable declaration outside the for loop.

double soma = 0;
for (int i = 0; i < bim; i++) {
     System.out.println("Insira a nota do " + (i + 1) + "º Bimestre");
     nota[i] = x.nextDouble();        
     soma = soma + nota[i];
}
    
29.08.2018 / 16:03
-2

In Java there is a strong typing, its soma variable has been internally declared in the for loop, ie it is visible only inside the loop. If you want to use it outside of the for loop, I advise you to declare it out of the loop:

public static void main(String[] args) {

    int tipo;

    int restante = 0;
    int bim = 4;
    double soma = 0;


    double[] nota = new double[bim];

    Scanner x = new Scanner(System.in);

    System.out.println("inisira a quatidade de bimestres");
    bim = x.nextInt();

    for (int i = 0; i < bim; i++) {
        System.out.println("Insira a nota do " + (i + 1) + "º Bimestre");
        nota[i] = x.nextDouble();
        soma = soma + nota[i];

    }

    double media = soma / bim;
    System.out.println(media);
}
    
06.09.2018 / 17:16