Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package _03Arrays_Exercise;
- import java.util.Scanner;
- public class _10TreasureHunt {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- String[] treasures = input.split("\\|");
- String stolenTreasures = "";
- String command = scanner.nextLine();
- while(!command.equals("Yohoho!")) {
- String[] treasureInput = command.split(" ");
- String treasureCmd = treasureInput[0];
- if(treasures.length >= 2 && treasures.length <= 100) {
- if (treasureCmd.equals("Loot")) {
- for (int i = 1; i < treasureInput.length; i++) {
- boolean isEqual = false;
- for (String treasure : treasures) {
- if (treasureInput[i].equals(treasure)) {
- isEqual = true;
- break;
- }
- }
- if (!isEqual) {
- String[] treasuresNew = new String[treasures.length + 1];
- for (int j = 0; j < treasures.length; j++) {
- treasuresNew[j] = treasures[j];
- }
- treasures = treasuresNew;
- // System.arraycopy(treasures, 0, treasuresNew, 0, treasures.length);
- for (int j = 0; j < treasures.length; j++) {
- if (j != treasures.length - 1) {
- treasures[treasures.length - 1 - j] = treasures[treasures.length - 2 - j];
- }
- }
- treasures[0] = treasureInput[i];
- }
- }
- } else if (treasureCmd.equals("Drop")) {
- int index = Integer.parseInt(treasureInput[1]);
- if (index >= 0 && index <= treasures.length - 1) {
- String droppedLoot = treasures[index];
- for (int i = index; i < treasures.length - 1; i++) {
- treasures[i] = treasures[i + 1];
- }
- treasures[treasures.length - 1] = droppedLoot;
- }
- } else if (treasureCmd.equals("Steal")) {
- int items = Integer.parseInt(treasureInput[1]);
- if (items >= 0 && items < treasures.length) {
- for (int i = treasures.length - items; i < treasures.length; i++) {
- stolenTreasures += treasures[i];
- if(i != treasures.length - 1) {
- stolenTreasures += ", ";
- }
- }
- String[] treasuresNew = new String[treasures.length - items];
- System.arraycopy(treasures, 0, treasuresNew, 0, treasuresNew.length);
- treasures = treasuresNew;
- } else {
- for (int i = 0; i < treasures.length; i++) {
- stolenTreasures += treasures[i];
- if(i != treasures.length - 1) {
- stolenTreasures += ", ";
- }
- }
- treasures = new String[0];
- }
- }
- }
- command = scanner.nextLine();
- }
- System.out.println(stolenTreasures);
- if(treasures.length > 0) {
- int sumItems = 0;
- for (String treasure : treasures) {
- sumItems += treasure.length();
- }
- double avgTreasureGain = sumItems * 1.0 / treasures.length;
- System.out.printf("Average treasure gain: %.2f pirate credits.", avgTreasureGain);
- } else {
- System.out.println("Failed treasure hunt.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement