Advertisement
svephoto

The Imitation Game [Java]

Aug 10th, 2021 (edited)
2,280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.15 KB | None | 0 0
  1. package Exams.FinalExams.ProgrammingFundamentalsFinalExam01;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class TheImitationGame {
  6.     public static void main(String[] args) {
  7.         Scanner scan = new Scanner(System.in);
  8.  
  9.         String message = scan.nextLine();
  10.  
  11.         String input = scan.nextLine();
  12.  
  13.         while (!input.equals("Decode")) {
  14.             String[] tokens = input.split("\\|");
  15.             String action = tokens[0];
  16.  
  17.             switch (action) {
  18.                 case "Move": { // <- На всички кейсове съм добавил къдрави отварящи и затварящи скоби, защото използвам еднакви имена на променливи в два от тях.
  19.                     int numLetters = Integer.parseInt(tokens[1]);
  20.  
  21.                     //Проверка за празен стринг
  22.                     //if (message.length() > 0 && numLetters < message.length()) { Всъщност, нямаш нужда от тези операции.
  23.                     //    StringBuilder strBuild = new StringBuilder(message);
  24.                     //    for (int i = 0; i < numLetters; i++) {
  25.                     //        strBuild.append(strBuild.charAt(0));
  26.                     //        strBuild.deleteCharAt(0);
  27.                     //    }
  28.                     //    message = strBuild.toString();
  29.                     // }
  30.                     // Може да го направиш направо така и доста си улесняваш живота :)
  31.                     String firstPart = message.substring(0, numLetters);
  32.                     String secondPart = message.substring(numLetters);
  33.                     message = secondPart.concat(firstPart);
  34.                     break;
  35.                 }
  36.                 case "Insert": {
  37.                     int index = Integer.parseInt(tokens[1]);
  38.                     String element = tokens[2];
  39.                     //Проверка за елемент
  40.                     // if (index >= 0 && index <= message.length()) {
  41.                     //    StringBuilder stringBuilder = new StringBuilder(message);
  42.                     //    stringBuilder.insert(index, element);
  43.                     //    message = stringBuilder.toString();
  44.                     //}
  45.  
  46.                     // И тук по подобен начин на горния кейс:
  47.                     String firstPart = message.substring(0, index);
  48.                     String secondPart = message.substring(index);
  49.                     message = firstPart.concat(element).concat(secondPart);
  50.                     break;
  51.                 }
  52.                 case "ChangeAll": {
  53.                     String substring = tokens[1];
  54.                     String replacement = tokens[2];
  55.                     //if (message.contains(substring)) { Тази проверка не ти е нужна и можеш да използваш просто replace вместо replaceAll.
  56.                     //}
  57.                     message = message.replace(substring, replacement);
  58.                     break;
  59.                 }
  60.             }
  61.  
  62.             input = scan.nextLine();
  63.         }
  64.  
  65.         System.out.printf("The decrypted message is: %s%n", message);
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement