Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Lists;
- import java.util.Scanner;
- public class ExamTest3 {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- String[] status1Input = scan.nextLine().split(">");
- String[] status2Input = scan.nextLine().split(">");
- int maxHealth = Integer.parseInt(scan.nextLine());
- int[] status1 = strToIntArr(status1Input);
- int[] status2 = strToIntArr(status2Input);
- String[] input = scan.nextLine().split(" ");
- while (!input[0].toLowerCase().equals("retire")) {
- String cmd = input[0];
- switch (cmd.toLowerCase()) {
- case "fire": {
- int index = Integer.parseInt(input[1]);
- int damage = Integer.parseInt(input[2]);
- if (index < 0 || index >= status2.length) {
- break;
- }
- int current = status2[index] - damage;
- if (current <= 0) {
- System.out.println("You won! The enemy ship has sunken.");
- return;
- } else {
- status2[index] = current;
- }
- break;
- }
- case "defend": {
- int startIndex = Integer.parseInt(input[1]);
- int endIndex = Integer.parseInt(input[2]);
- int damage = Integer.parseInt(input[3]);
- if (startIndex < 0 || startIndex >= status1.length) {
- break;
- }
- if (endIndex < 0 || endIndex >= status1.length) {
- break;
- }
- for (int i = startIndex; i <= endIndex; i++) {
- int current = status1[i] - damage;
- if (current <= 0) {
- System.out.println("You lost! The pirate ship has sunken.");
- return;
- } else {
- status1[i] = current;
- }
- }
- break;
- }
- case "repair": {
- int index = Integer.parseInt(input[1]);
- int health = Integer.parseInt(input[2]);
- if (index < 0 || index >= status1.length) {
- break;
- }
- int current = status1[index] + health;
- if (current > maxHealth) current = maxHealth;
- status1[index] = current;
- break;
- }
- case "status": {
- int sum = 0;
- for (int i = 0; i < status1.length; i++) {
- if (status1[i] < 0.2 * maxHealth) sum++;
- }
- System.out.printf("%d sections need repair.%n", sum);
- break;
- }
- }
- input = scan.nextLine().split(" ");
- }
- int sum1 = 0;
- int sum2 = 0;
- for (int item : status1) {
- sum1 += item; //sum1 = sum1 + item
- }
- for (int item : status2) {
- sum2 += item;
- }
- System.out.printf("Pirate ship status: %d%nWarship status: %d%n", sum1, sum2);
- }
- private static int[] strToIntArr(String[] status1Input) {
- int[] arr = new int[status1Input.length];
- for (int i = 0; i < arr.length; i++) {
- arr[i] = Integer.parseInt(status1Input[i]);
- }
- return arr;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement