Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Arrays.Exercise;
- import java.util.Scanner;
- public class P10TreasureHunt {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] lootArr = scanner.nextLine().split("\\|");
- String command = scanner.nextLine();
- while (!command.equals("Yohoho!")) {
- String[] commandArr = command.split(" ");
- if (command.contains("Loot")) {
- for (int i = 1; i < commandArr.length; i++) {
- boolean alreadyContained = false;
- for (int j = 0; j < lootArr.length; j++) {
- if (commandArr[i].equals(lootArr[j])) {
- alreadyContained = true;
- break;
- }
- }
- if (!alreadyContained) {
- String newItem = commandArr[i] + " " + String.join(" ", lootArr);
- lootArr = newItem.split(" ");
- }
- }
- } else if (command.contains("Drop")) {
- int index = Integer.parseInt(commandArr[1]);
- if (index <= lootArr.length - 1 && index >= 0) {
- String droppedItem = lootArr[index];
- for (int j = index; j < lootArr.length - 1; j++) {
- lootArr[j] = lootArr[j + 1];
- }
- lootArr[lootArr.length - 1] = droppedItem;
- } else {
- command = scanner.nextLine();
- continue;
- }
- } else if (command.contains("Steal")) {
- int index = Integer.parseInt(commandArr[1]);
- if (index > lootArr.length) {
- index = lootArr.length;
- }
- String[] stealArr = new String[0];
- for (int i = lootArr.length - 1; i >= lootArr.length - index; i--) {
- String stolenItem = lootArr[i] + " " + String.join(" ", stealArr);
- stealArr = stolenItem.split(" ");
- }
- for (int j = 0; j < stealArr.length; j++) {
- String currentItem = stealArr[j];
- if (j != stealArr.length - 1) {
- System.out.print(currentItem + ", ");
- } else {
- System.out.print(currentItem);
- }
- }
- String [] newChest = new String[lootArr.length - stealArr.length];
- for (int i = 0; i < newChest.length; i++) {
- String leftItem = lootArr[i];
- newChest[i] = leftItem;
- }
- lootArr = newChest;
- }
- command = scanner.nextLine();
- }
- int totalLength = 0;
- int itemsCount = lootArr.length;
- if (itemsCount == 0) {
- System.out.printf("%nFailed treasure hunt.");
- } else {
- for (int i = 0; i < lootArr.length; i++) {
- String currentItem = lootArr[i];
- int itemLength = currentItem.length();
- totalLength+=itemLength;
- }
- double averageHunt = (totalLength * 1.0) / itemsCount;
- System.out.printf("%nAverage treasure gain: %.2f pirate credits.", averageHunt);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement