Question 1-3

Fill in run() so that, when executed, the method will count the number of positive numbers the user types before the user types 0. A user executing the method should see the following, for example. (Boldface indicates what the user types.)

123
-2
8
123
0
3
In this case, the program displays 3, because the user typed three positive numbers (123, 8, and 123).
import csbsju.cs160.*;
public class CountPositives {
    public static void run() {



    }
}

Solution

import csbsju.cs160.*;
public class CountPositives {
    public static void run() {
        int count = 0;
        while(true) {
            int n = IO.readInt();
            if(n == 0) break;
            if(n > 0) ++count;
        }
        IO.println(count);
    }
}

Back to 8:00 Quiz 1