Lukas Demeyere Lukas Demeyere - 1 year ago 101
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"));

total = quantity + quantity;


Answer Source


total = quantity + quantity;


total += quantity;

otherwise total is reset each loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download