Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package MidExam.O2;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class O2ArrayModifier {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<Integer> test = Arrays.stream(scanner.nextLine().split("\\s+")).
- map(Integer::parseInt).collect(Collectors.toList());
- String input = scanner.nextLine();
- while (!input.equals("end")) {
- String command = input.split(" ")[0];
- switch (command) {
- case "swap":
- case "multiply":
- int index = Integer.parseInt(input.split(" ")[1]);
- int index2 = Integer.parseInt(input.split(" ")[2]);
- int a = test.get(index);
- int b = test.get(index2);
- if (command.equals("swap")){
- test.set(index, b);
- test.set(index2, a);
- }else{
- int sum = a * b;
- test.set(index, sum);
- }
- break;
- case "decrease":
- for (int i = 0; i < test.size(); i++) {
- test.set(i, test.get(i) - 1);
- }
- break;
- }
- input = scanner.nextLine();
- }
- for (int i = 0; i < test.size() - 1; i++) {
- System.out.print(test.get(i) + ", ");
- }
- System.out.print(test.get(test.size() - 1));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment