Lukas Demeyere Lukas Demeyere - 3 months ago 11
Java Question

While loop counting java

I am trying to make a program which asks you how many persons there are and asks for each invidual person how many objects they want. I'm using a while loop but I don't know how to make a total. It has to count the amounts you put in.

I've searched for it, but couldn't find out how to do it.

public class BakkenTeller {
public static void main(String [ ] args) {
int i = 0;
int quantity = 0;
int total = 0;

JFrame venster = new JFrame("Hoeveel mensen zijn er");

int personen = Integer.parseInt(JOptionPane.showInputDialog(venster, "Met hoeveel zijn jullie"));

while (i < personen) {
quantity = Integer.parseInt(JOptionPane.showInputDialog(venster, "Hoeveel pintjes zal persoon " + (i + 1) + " consumeren"));
i++;

total = quantity + quantity;
}
System.out.println(total);
}


}

Answer

change

total = quantity + quantity;

to

total += quantity;

otherwise total is reset each loop