Question 1-4

Write a method that reads in a sequence of integers from the user, terminated by a 0, and displays the sum of the numbers typed. Here's an example transcript (user input in boldface).

4
2
-3
0
3
import csbsju.cs160.*;

public class FindSum {
    public static void run() {
        // your code here...



    }
}

Solution

import csbsju.cs160.*;

public class FindSum {
    public static void run() {
        int total = 0;
        while(true) {
            int input = IO.readInt();
            if(input == 0) break;
            total += input;
        }
        IO.println(total);
    }
}

Back to Review for Quiz 1