Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package MidExamRetake;
- import org.w3c.dom.xpath.XPathResult;
- import java.lang.reflect.Array;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Scanner;
- public class ManOWar {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] input1 = scanner.nextLine().split(">");
- String[] input2 = scanner.nextLine().split(">");
- int maxHealth = Integer.parseInt(scanner.nextLine());
- int count = 0;
- int pirateShipSum = 0;
- int warshipShum = 0;
- ArrayList<Integer> pirateship = new ArrayList<>();
- ArrayList<Integer> warship = new ArrayList<>();
- for (int i = 0; i < input1.length; i++) {
- pirateship.add(Integer.parseInt(input1[i]));
- }
- for (int i = 0; i < input2.length; i++) {
- warship.add(Integer.parseInt(input2[i]));
- }
- String input = scanner.nextLine();
- while (!input.equals("Retire")) {
- String[] tokens = input.split(" ");
- String command = tokens[0];
- switch (command) {
- case "Fire":
- int index = Integer.parseInt(tokens[1]);
- int damage = Integer.parseInt(tokens[2]);
- if (0 <= index && index <= warship.size()) {
- warship.set(index, warship.get(index) - damage);
- }else{
- break;
- }
- if (warship.get(index) <= 0) {
- System.out.print("You won! The enemy ship has sunken.");
- break;
- }
- break;
- case "Defend":
- int startIndex = Integer.parseInt(tokens[1]);
- int endIndex = Integer.parseInt(tokens[2]);
- damage = Integer.parseInt(tokens[3]);
- if (startIndex >= 0 && endIndex <= pirateship.size()) {
- for (int i = startIndex; i <= endIndex; i++) {
- pirateship.set(i, pirateship.get(i) - damage);
- if (pirateship.get(i) <= 0) {
- System.out.print("You lost! The pirate ship has sunken.");
- break;
- }
- }
- }
- break;
- case "Repair":
- index = Integer.parseInt(tokens[1]);
- int givenHealth = Integer.parseInt(tokens[2]);
- if (0 <= index && index <= warship.size()) {
- pirateship.set(index, pirateship.get(index) + givenHealth);
- if (pirateship.get(index) > maxHealth) {
- pirateship.set(index, maxHealth);
- }
- }
- break;
- case "Status":
- for (int i = 0; i < pirateship.size(); i++) {
- if (pirateship.get(i) < 0.2*maxHealth) {
- count++;
- }
- }
- System.out.printf("%d sections need repair.%n", count);
- break;
- }
- input = scanner.nextLine();
- }
- for (int i = 0; i < pirateship.size(); i++) {
- pirateShipSum += pirateship.get(i);
- }
- for (int i = 0; i < warship.size(); i++) {
- warshipShum += warship.get(i);
- }
- System.out.println("Pirate ship status: "+ pirateShipSum);
- System.out.print("Warship status: "+warshipShum);
- }
- }
Add Comment
Please, Sign In to add comment