Skip to content

Instantly share code, notes, and snippets.

@charsyam
Created June 11, 2018 17:20
Show Gist options
  • Save charsyam/48a8a0fe56e2eb480076b8b8574c5bed to your computer and use it in GitHub Desktop.
Save charsyam/48a8a0fe56e2eb480076b8b8574c5bed to your computer and use it in GitHub Desktop.
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
public class SumNumber {
public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
List<Integer> values = new ArrayList<Integer>();
while(true) {
String line = sc.nextLine();
if ("q".equals(line)) {
break;
}
String [] parts = line.split(" ");
switch(parts[0]) {
case "p":
for (int i : values) {
System.out.println(i);
}
break;
case "s":
int sum = 0;
for (int i : values) {
sum += i;
}
System.out.println(sum);
values.clear();
values.add(sum);
break;
case "a":
int num = Integer.valueOf(parts[1]);
values.add(num);
break;
case "d":
int last = values.size() - 1;
values.remove(last);
break;
default:
System.out.println("Unsupported : " + line);
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment