Get number of entries inside JOptionPane

0

Hello, I'm having trouble getting the number of entries that the user makes and then using them.

Code:

public static void main(String[] args) {
    // TODO code application logic here
    int n, s = 0, p, i, c, m;
    do {
      n = Integer.parseInt(JOptionPane.showInputDialog(null, "<html>Informe um número: <br><em>(Valor 0 interrompe)</em></html>"));
      s += n;
    } while (n != 0);
    JOptionPane.showMessageDialog(null, "<html>Resultado: <hr><br>" +
    "Soma de Valores: " + s + "<br>Total de Pares:" +
    "<br>Total de Ímpares:" + "<br>Acima de 100:" +
    "<br>Média de Valores: </html>");

}

Explanation of variables

n -> número informado
s -> soma dos números
p -> valores pares
i -> valores ímpares
c -> números acima de 100
m -> média

Okay, the problem would be: How do I get the number of entries that the user gave?

For example, it uses the following entries: 2, 12, 31, 47, 132, 0 ;

In other words, for me to average, I should take the total and divide by the number of entries:

224 (sum) / 5 (number of entries, excluding 0)

How do I get him to count the number of tickets to use?

    
asked by anonymous 16.09.2016 / 02:36

2 answers

1
One of the possible solutions, thinking in a simplistic way, would be to add a counter and check if n is equal to 0, if not, it increases:

int n, s = 0, p, i, c, m, contador = 0;

do {
  n = Integer.parseInt(JOptionPane.showInputDialog(null, "<html>Informe um número: <br><em>(Valor 0 interrompe)</em></html>"));
  s += n;

  if(n != 0){
     contador++;
  }

} while (n != 0);
    
16.09.2016 / 03:05
-1

I ended up developing the whole program this way:

    int n, s = 0, p = -1, i = 0, c = 0, m = -1, e = -1;
    do {
      n = Integer.parseInt(JOptionPane.showInputDialog(null, "<html>Informe um número: <br><em>(Valor 0 interrompe)</em></html>"));
      s += n;
      m++;
      e++;
      if (n % 2 == 0) {
          p++;
    } else {
          i++;    
      }
      if (n >= 100){
          c++;
      }
    } while (n != 0);
    int media = s / m;
    JOptionPane.showMessageDialog(null, "<html>Resultado: <hr><br>" + "Número de Entradas: " +
            e + "<br>Soma de Valores: " + s + "<br>Total de Pares: " + p + "<br>Total de Ímpares: " +
            i + "<br>Acima de 100: " + c + "<br>Média de Valores: " + media + "</html>"); 

If you have simpler ways, please edit.

    
16.09.2016 / 03:22