deiom

Untitled

Aug 6th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. package MidExamRetake;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Scanner;
  6.  
  7. public class TreasureHunt {
  8. public static void main(String[] args) {
  9. Scanner scanner = new Scanner(System.in);
  10.  
  11. String[] input = scanner.nextLine().split("\\|");
  12. ArrayList<String> chest = new ArrayList<>(Arrays.asList(input));
  13. ArrayList<String> stolenItems = new ArrayList<>();
  14. double averageGain = 0.0;
  15. double lenght = 0;
  16. String nextInput = scanner.nextLine();
  17.  
  18. while (!nextInput.equals("Yohoho!")) {
  19. String[] tokens = nextInput.split(" ");
  20. String command = tokens[0];
  21.  
  22. switch (command) {
  23. case "Loot":
  24. for (int i = 1; i < tokens.length; i++) {
  25. String newLoot = tokens[i];
  26. if (!chest.contains(newLoot)) {
  27. chest.add(0, newLoot);
  28. }
  29. }
  30. break;
  31. case "Drop":
  32. int index = Integer.parseInt(tokens[1]);
  33. if(-22<=index && index<=200) {
  34. if (0 <= index && index < chest.size()) {
  35. String item = chest.get(index);
  36. chest.add(item);
  37. chest.remove(index);
  38. }
  39. }
  40. break;
  41. case "Steal": {
  42. int count = Integer.parseInt(tokens[1]);
  43. for (int i = chest.size()- count; i<chest.size(); i++) {
  44. stolenItems.add(chest.get(i));
  45. chest.remove(i);
  46. i--;
  47. }
  48. }
  49. break;
  50. }
  51. nextInput = scanner.nextLine();
  52. }
  53.  
  54. for (int i = 0; i < stolenItems.size() - 1; i++) {
  55. System.out.printf("%s, ", stolenItems.get(i));
  56. }
  57. for (int i = stolenItems.size() - 1; i < stolenItems.size(); i++) {
  58. System.out.println(stolenItems.get(i));
  59. }
  60. for (int i = 0; i < chest.size(); i++) {
  61. lenght += chest.get(i).length();
  62. ;
  63. }
  64. double counter = chest.size();
  65. averageGain = lenght / counter;
  66.  
  67. if (chest.size() != 0) {
  68. System.out.printf("Average treasure gain: %.2f pirate credits.", averageGain);
  69. } else {
  70. System.out.println("Failed treasure hunt.");
  71. }
  72. }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment