Skip to content

Instantly share code, notes, and snippets.

@michaelee
Created April 8, 2009 18:10
Show Gist options
  • Save michaelee/91899 to your computer and use it in GitHub Desktop.
Save michaelee/91899 to your computer and use it in GitHub Desktop.
import java.util.*;
public class Accumulator {
private int accum = 0;
public void add(int val) {
accum = accum + val;
}
public int getValue() {
return accum;
}
public static void main(String [] args) {
Scanner scanner = new Scanner(System.in);
Accumulator accum = new Accumulator();
int input;
boolean finished = false;
while (!finished) {
System.out.print("Enter a value: ");
input = scanner.nextInt();
if (input == 0) {
finished = true;
} else {
accum.add(input);
}
}
System.out.format("Accumulated sum: %d\n", accum.getValue());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment