Created
April 8, 2009 18:10
-
-
Save michaelee/91899 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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