Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package MidExamRetake;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Scanner;
- public class TreasureHunt {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] input = scanner.nextLine().split("\\|");
- ArrayList<String> chest = new ArrayList<>(Arrays.asList(input));
- ArrayList<String> stolenItems = new ArrayList<>();
- double averageGain = 0.0;
- double lenght = 0;
- String nextInput = scanner.nextLine();
- while (!nextInput.equals("Yohoho!")) {
- String[] tokens = nextInput.split(" ");
- String command = tokens[0];
- switch (command) {
- case "Loot":
- for (int i = 1; i < tokens.length; i++) {
- String newLoot = tokens[i];
- if (!chest.contains(newLoot)) {
- chest.add(0, newLoot);
- }
- }
- break;
- case "Drop":
- int index = Integer.parseInt(tokens[1]);
- if(-22<=index && index<=200) {
- if (0 <= index && index < chest.size()) {
- String item = chest.get(index);
- chest.add(item);
- chest.remove(index);
- }
- }
- break;
- case "Steal": {
- int count = Integer.parseInt(tokens[1]);
- for (int i = chest.size()- count; i<chest.size(); i++) {
- stolenItems.add(chest.get(i));
- chest.remove(i);
- i--;
- }
- }
- break;
- }
- nextInput = scanner.nextLine();
- }
- for (int i = 0; i < stolenItems.size() - 1; i++) {
- System.out.printf("%s, ", stolenItems.get(i));
- }
- for (int i = stolenItems.size() - 1; i < stolenItems.size(); i++) {
- System.out.println(stolenItems.get(i));
- }
- for (int i = 0; i < chest.size(); i++) {
- lenght += chest.get(i).length();
- ;
- }
- double counter = chest.size();
- averageGain = lenght / counter;
- if (chest.size() != 0) {
- System.out.printf("Average treasure gain: %.2f pirate credits.", averageGain);
- } else {
- System.out.println("Failed treasure hunt.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment