Created
June 11, 2018 17:20
-
-
Save charsyam/48a8a0fe56e2eb480076b8b8574c5bed to your computer and use it in GitHub Desktop.
This file contains hidden or 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.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