Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package MidExam.O6;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class O3ManOnWar {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<Integer> pirateShip = Arrays.stream(scanner.nextLine().split(">")).
- map(Integer::parseInt).collect(Collectors.toList());
- List<Integer> warship = Arrays.stream(scanner.nextLine().split(">")).
- map(Integer::parseInt).collect(Collectors.toList());
- int maxHealth = Integer.parseInt(scanner.nextLine());
- String command = scanner.nextLine();
- while (!command.equals("Retire")) {
- String type = command.split("\\s+")[0];
- switch (type) {
- case "Fire":
- int index1 = Integer.parseInt(command.split("\\s+")[1]);
- int index2 = Integer.parseInt(command.split("\\s+")[2]);
- if (index1 >= 0 && index1 < warship.size()) {
- warship.set(index1, warship.get(index1) - index2);
- if (warship.get(index1) <= 0) {
- System.out.println("You won! The enemy ship has sunken.");
- return;
- }
- }
- break;
- case "Defend":
- index1 = Integer.parseInt(command.split("\\s+")[1]);
- index2 = Integer.parseInt(command.split("\\s+")[2]);
- int index3 = Integer.parseInt(command.split("\\s+")[3]);
- if (index1 >= 0 && index1 < pirateShip.size()&& index2 >= 0 && index2 < pirateShip.size()) {
- for (int i = index1; i <= index2; i++) {
- pirateShip.set(i, pirateShip.get(i)-index3);
- if (pirateShip.get(i) <= 0) {
- System.out.println("You lost! The pirate ship has sunken.");
- return;
- }
- }
- }
- break;
- case "Repair":
- index1 = Integer.parseInt(command.split("\\s+")[1]);
- index2 = Integer.parseInt(command.split("\\s+")[2]);
- if (index1 >= 0 && index1 < pirateShip.size()) {
- pirateShip.set(index1, pirateShip.get(index1) + index2);
- if (pirateShip.get(index1) > maxHealth) {
- pirateShip.set(index1,maxHealth);
- }
- }
- break;
- case "Status":
- int count = 0;
- for (Integer integer : pirateShip) {
- if (integer < 0.2 * maxHealth) {
- count++;
- }
- }
- System.out.println(count + " sections need repair.");
- break;
- }
- command = scanner.nextLine();
- }
- int pirateShipStatus = 0;
- for (Integer currentStatus : pirateShip) {
- pirateShipStatus += currentStatus;
- }
- int warShipStatus = 0;
- for (Integer currentStatus : warship) {
- warShipStatus += currentStatus;
- }
- System.out.println("Pirate ship status: " + pirateShipStatus);
- System.out.println("Warship status: " + warShipStatus);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment