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

Loop scheme for a counter: number of positive integers

Count the number of positive integers in input.

String s;       // current string in input
int counter;

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

System.out.println("Number of positive integers in input = " + counter);

In this case, the increment of the counter depends on a condition.


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