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

Loop scheme for an accumulator: sum of integers

Sum the integers in input.

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

sum = 0;
s = JOptionPane.showInputDialog("Input an integer");
while (s != null) {
  n = Integer.parseInt(s);
  sum = sum + n;
  s = JOptionPane.showInputDialog("Input an integer");
}

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


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