Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ThirdTaskLists;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class MovingTarget {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<Integer> targets = Arrays.stream(scanner.nextLine().split("\\s+"))
- .map(Integer::parseInt)
- .collect(Collectors.toList());
- String command = scanner.nextLine();
- while (!command.equals("End")) {
- if (command.contains("Shoot")) {
- int shootIndex = Integer.parseInt(command.split("\\s+")[1]);
- int shootPower = Integer.parseInt(command.split("\\s+")[2]);
- if (shootIndex >= 0 && shootIndex < targets.size()) { //->има ли index //changed
- int newValue = targets.get(shootIndex);
- newValue = newValue - shootPower;
- if (newValue <= 0) {
- targets.remove(shootIndex);
- } else {
- targets.set(shootIndex, newValue);
- }
- }
- } else if (command.contains("Add")) {
- int addIndex = Integer.parseInt(command.split("\\s+")[1]);
- int addValue = Integer.parseInt(command.split("\\s+")[2]);
- if (addIndex >= 0 && addIndex < targets.size()) {
- targets.add(addIndex, addValue);
- } else {
- System.out.println("Invalid placement!");
- }
- } else if (command.contains("Strike")) {
- int strikeIndex = Integer.parseInt(command.split("\\s+")[1]);
- int radius = Integer.parseInt(command.split("\\s+")[2]);
- if (strikeIndex < 0
- || strikeIndex >= targets.size()
- || radius == 0
- || radius > strikeIndex
- || strikeIndex + radius >= targets.size()) {
- System.out.println("Strike missed!");
- } else {
- targets.subList(strikeIndex - radius, strikeIndex + radius + 1).clear(); //changed
- }
- }
- command = scanner.nextLine();
- }
- for (int i = 0; i < targets.size() - 1; i++) {
- System.out.print(targets.get(i) + "|");
- }
- System.out.print(targets.get(targets.size() - 1));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment