Advertisement
desislava_topuzakova

Untitled

Jan 27th, 2023
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. package Arrays;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class TreasureHunt_10 {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. String [] loots = scanner.nextLine().split("\\|");
  9. //"Gold|Silver|Bronze|Medallion|Cup".split("|") -> ["Gold", "Silver", "Bronze", "Medallion", "Cup"]
  10.  
  11. String command = scanner.nextLine(); //цялата команда, въведена от конзолата
  12.  
  13. while (!command.equals("Yohoho!")) {
  14. //command = "Loot {item1} {item2}…{itemn}".split -> ["Loot", "itrem1", "item2", ...]
  15.  
  16. //command = "Steal 2".split -> ["Steal", "2"]
  17. String[] commandParts = command.split(" "); //частите на командата
  18. String commandName = commandParts[0]; //име на команда: Loot, Drop, Steal
  19.  
  20. switch (commandName) {
  21. case "Loot":
  22. break;
  23. case "Drop":
  24. //command = "Drop 4".split -> commandParts = ["Drop", "4"]
  25. int dropIndex = Integer.parseInt(commandParts[1]); //"4" -> 4
  26. //проверка на индекс
  27. if(dropIndex < 0 || dropIndex >= loots.length - 1) {
  28. //невалиден индекс -> индекс, на който нямаме елемент
  29. break;
  30. } else{
  31. //валиден индекс
  32. //1. взимаме елемента за преместване
  33. String currentLoot = loots[dropIndex];
  34. //2. премествам на ляво всички елементи след моя
  35. for (int leftIndex = dropIndex; leftIndex < loots.length - 1; leftIndex++) {
  36. loots[leftIndex] = loots[leftIndex + 1];
  37. }
  38. //3. слагаме накрая елемента за преместване
  39. loots[loots.length - 1] = currentLoot;
  40. }
  41. break;
  42. case "Steal":
  43. break;
  44. }
  45.  
  46. command = scanner.nextLine();
  47. }
  48. }
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement