next up previous
Next: Loop scheme for an Up: Unit 06 Previous: Loop scheme for an

Loop scheme for an accumulator: product of integers

Multiply the integers in input.

String s;       // current input string
int n;          // current integer
int product;    // variable used as accumulator

product = 1;    // 1 is the neutral element for multiplication
s = JOptionPane.showInputDialog("Input an integer");
while (s != null) {
  n = Integer.parseInt(s);
  product = product * n;
  s = JOptionPane.showInputDialog("Input an integer");
}

System.out.println("product = " + product);


next up previous
Next: Loop scheme for an Up: Unit 06 Previous: Loop scheme for an