Advertisement
svephoto

Warrior's Quest [Java]

Mar 5th, 2020
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.32 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class WarriorsQuest {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         String input = scanner.nextLine();
  8.  
  9.         String command;
  10.  
  11.         while (!"For Azeroth".equals(command = scanner.nextLine())) {
  12.             String[] tokens = command.split("\\s+");
  13.  
  14.             if ("GladiatorStance".equals(tokens[0])) {
  15.                 input = input.toUpperCase();
  16.                 System.out.println(input);
  17.             } else if ("DefensiveStance".equals(tokens[0])) {
  18.                 input = input.toLowerCase();
  19.                 System.out.println(input);
  20.             } else if ("Dispel".equals(tokens[0])) {
  21.                 int index = Integer.parseInt(tokens[1]);
  22.                 if (index >= 0 && index < input.length()) {
  23.                     String letterToReplace = input.charAt(index) + "";
  24.                     String replacementLetter = tokens[2].charAt(0) + "";
  25.                     input = input.replace(letterToReplace, replacementLetter);
  26.                     System.out.println("Success!");
  27.                 } else {
  28.                     System.out.println("Dispel too weak.");
  29.                 }
  30.             } else if ("Target".equals(tokens[0]) && "Change".equals(tokens[1])) {
  31.                 String firstSubstring = tokens[2];
  32.                 String secondSubstring = tokens[3];
  33.                 if (input.contains(firstSubstring)) {
  34.                     input = input.replace(firstSubstring, secondSubstring);
  35.                     System.out.println(input);
  36.                 } else {
  37.                     break;
  38.                 }
  39.             } else if ("Target".equals(tokens[0]) && "Remove".equals(tokens[1])) {
  40.                 StringBuilder builder = new StringBuilder(input);
  41.                 String target = tokens[2];
  42.  
  43.                 if (input.contains(target)) {
  44.                     int startIndex = input.indexOf(target);
  45.                     int stopIndex = startIndex + target.length();
  46.  
  47.                     builder.delete(startIndex, stopIndex);
  48.                     input = "" + builder;
  49.  
  50.                     System.out.println(input);
  51.                 } else {
  52.                     break;
  53.                 }
  54.             } else {
  55.                 System.out.println("Command doesn't exist!");
  56.             }
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement