deiom

Untitled

Aug 6th, 2019
603
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. package MidExamRetake;
  2.  
  3. import org.w3c.dom.xpath.XPathResult;
  4.  
  5. import java.lang.reflect.Array;
  6. import java.util.ArrayList;
  7. import java.util.Arrays;
  8. import java.util.Scanner;
  9.  
  10. public class ManOWar {
  11. public static void main(String[] args) {
  12. Scanner scanner = new Scanner(System.in);
  13.  
  14. String[] input1 = scanner.nextLine().split(">");
  15. String[] input2 = scanner.nextLine().split(">");
  16. int maxHealth = Integer.parseInt(scanner.nextLine());
  17. int count = 0;
  18. int pirateShipSum = 0;
  19. int warshipShum = 0;
  20.  
  21. ArrayList<Integer> pirateship = new ArrayList<>();
  22. ArrayList<Integer> warship = new ArrayList<>();
  23.  
  24. for (int i = 0; i < input1.length; i++) {
  25. pirateship.add(Integer.parseInt(input1[i]));
  26. }
  27. for (int i = 0; i < input2.length; i++) {
  28. warship.add(Integer.parseInt(input2[i]));
  29. }
  30.  
  31. String input = scanner.nextLine();
  32.  
  33. while (!input.equals("Retire")) {
  34. String[] tokens = input.split(" ");
  35. String command = tokens[0];
  36.  
  37.  
  38. switch (command) {
  39. case "Fire":
  40. int index = Integer.parseInt(tokens[1]);
  41. int damage = Integer.parseInt(tokens[2]);
  42. if (0 <= index && index <= warship.size()) {
  43. warship.set(index, warship.get(index) - damage);
  44.  
  45. }else{
  46. break;
  47. }
  48. if (warship.get(index) <= 0) {
  49. System.out.print("You won! The enemy ship has sunken.");
  50. break;
  51. }
  52. break;
  53. case "Defend":
  54. int startIndex = Integer.parseInt(tokens[1]);
  55. int endIndex = Integer.parseInt(tokens[2]);
  56. damage = Integer.parseInt(tokens[3]);
  57. if (startIndex >= 0 && endIndex <= pirateship.size()) {
  58. for (int i = startIndex; i <= endIndex; i++) {
  59. pirateship.set(i, pirateship.get(i) - damage);
  60. if (pirateship.get(i) <= 0) {
  61. System.out.print("You lost! The pirate ship has sunken.");
  62. break;
  63. }
  64. }
  65. }
  66.  
  67. break;
  68. case "Repair":
  69. index = Integer.parseInt(tokens[1]);
  70. int givenHealth = Integer.parseInt(tokens[2]);
  71. if (0 <= index && index <= warship.size()) {
  72. pirateship.set(index, pirateship.get(index) + givenHealth);
  73. if (pirateship.get(index) > maxHealth) {
  74. pirateship.set(index, maxHealth);
  75. }
  76. }
  77. break;
  78. case "Status":
  79. for (int i = 0; i < pirateship.size(); i++) {
  80. if (pirateship.get(i) < 0.2*maxHealth) {
  81. count++;
  82. }
  83. }
  84. System.out.printf("%d sections need repair.%n", count);
  85. break;
  86.  
  87. }
  88. input = scanner.nextLine();
  89. }
  90.  
  91. for (int i = 0; i < pirateship.size(); i++) {
  92. pirateShipSum += pirateship.get(i);
  93. }
  94. for (int i = 0; i < warship.size(); i++) {
  95. warshipShum += warship.get(i);
  96. }
  97. System.out.println("Pirate ship status: "+ pirateShipSum);
  98. System.out.print("Warship status: "+warshipShum);
  99.  
  100. }
  101. }
Add Comment
Please, Sign In to add comment