Advertisement
george1119

Untitled

Jul 4th, 2020
1,202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.70 KB | None | 0 0
  1. package Lists;
  2.  
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class ExamTest3 {
  7.     public static void main(String[] args) {
  8.         Scanner scan = new Scanner(System.in);
  9.  
  10.         String[] status1Input = scan.nextLine().split(">");
  11.         String[] status2Input = scan.nextLine().split(">");
  12.  
  13.         int maxHealth = Integer.parseInt(scan.nextLine());
  14.  
  15.         int[] status1 = strToIntArr(status1Input);
  16.         int[] status2 = strToIntArr(status2Input);
  17.  
  18.         String[] input = scan.nextLine().split(" ");
  19.         while (!input[0].toLowerCase().equals("retire")) {
  20.             String cmd = input[0];
  21.  
  22.  
  23.             switch (cmd.toLowerCase()) {
  24.                 case "fire": {
  25.                     int index = Integer.parseInt(input[1]);
  26.                     int damage = Integer.parseInt(input[2]);
  27.                     if (index < 0 || index >= status2.length) {
  28.                         break;
  29.                     }
  30.                     int current = status2[index] - damage;
  31.  
  32.                     if (current <= 0) {
  33.                         System.out.println("You won! The enemy ship has sunken.");
  34.                         return;
  35.                     } else {
  36.                         status2[index] = current;
  37.                     }
  38.  
  39.                     break;
  40.                 }
  41.  
  42.                 case "defend": {
  43.                     int startIndex = Integer.parseInt(input[1]);
  44.                     int endIndex = Integer.parseInt(input[2]);
  45.                     int damage = Integer.parseInt(input[3]);
  46.  
  47.                     if (startIndex < 0 || startIndex >= status1.length) {
  48.                         break;
  49.                     }
  50.                     if (endIndex < 0 || endIndex >= status1.length) {
  51.                         break;
  52.                     }
  53.  
  54.                     for (int i = startIndex; i <= endIndex; i++) {
  55.                         int current = status1[i] - damage;
  56.  
  57.                         if (current <= 0) {
  58.                             System.out.println("You lost! The pirate ship has sunken.");
  59.                             return;
  60.                         } else {
  61.                             status1[i] = current;
  62.                         }
  63.                     }
  64.  
  65.                     break;
  66.                 }
  67.  
  68.                 case "repair": {
  69.                     int index = Integer.parseInt(input[1]);
  70.                     int health = Integer.parseInt(input[2]);
  71.  
  72.                     if (index < 0 || index >= status1.length) {
  73.                         break;
  74.                     }
  75.  
  76.                     int current = status1[index] + health;
  77.                     if (current > maxHealth) current = maxHealth;
  78.                     status1[index] = current;
  79.  
  80.                     break;
  81.                 }
  82.  
  83.                 case "status": {
  84.                     int sum = 0;
  85.                     for (int i = 0; i < status1.length; i++) {
  86.                         if (status1[i] < 0.2 * maxHealth) sum++;
  87.                     }
  88.                     System.out.printf("%d sections need repair.%n", sum);
  89.                     break;
  90.                 }
  91.             }
  92.  
  93.             input = scan.nextLine().split(" ");
  94.         }
  95.  
  96.         int sum1 = 0;
  97.         int sum2 = 0;
  98.         for (int item : status1) {
  99.             sum1 += item; //sum1 = sum1 + item
  100.         }
  101.         for (int item : status2) {
  102.             sum2 += item;
  103.         }
  104.  
  105.         System.out.printf("Pirate ship status: %d%nWarship status: %d%n", sum1, sum2);
  106.     }
  107.  
  108.     private static int[] strToIntArr(String[] status1Input) {
  109.         int[] arr = new int[status1Input.length];
  110.  
  111.         for (int i = 0; i < arr.length; i++) {
  112.             arr[i] = Integer.parseInt(status1Input[i]);
  113.         }
  114.  
  115.         return arr;
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement