Pick up quantity of entries inside the Joptionpane

Asked

Viewed 399 times

0

Hello, I’m having difficulty picking up the number of entries that is made by the user to then use 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

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

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

I mean, for me to make the average I should take the total and divide by the number of entries:

224 (summing up) / 5 (number of entries, excluding 0)

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

  • 1

    They are two very different questions together, I recommend separating them, even because the second one is a little confused. Perhaps separating it into a new one you can explain better without making the question broad.

  • Okay, I corrected the question and left the main theme

2 answers

1


One of the possible solutions, thinking simplistically, would be to add a counter and check if n is equal to 0, if not, increment:

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);
  • I ended up using a variable and and added a and++; ao while. I also put it as the initial value -1, so when the user puts 0 will not have "enter" 0.

-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 correct.

  • If m is your counter, it is wrong, starting with -1 will not return the correct number of entries, and consequently your average will also be wrong. See the answer I made below.

  • Actually it will not "reset" the counter when the user puts the 0 to interrupt the program?

  • 1

    No, he’s gonna be one number down, his media’s always gonna be wrong. If you want all entries but zero, here’s how I did the validation in the answer. Not to mention that this division has a serious risk of releasing a aritmethicexception, because if the second entry is zero, your counter will also be zero and your program will attempt an impossible split.

Browser other questions tagged

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