Kevil_Karnage

11 15

Feb 22nd, 2020
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.44 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. import java.lang.String;
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner scn = new Scanner(System.in);
  9.         System.out.println("Введите код на языке C#: ");
  10.         String[] readCode = readLinesFromConsole();
  11.         deleteComment(readCode);
  12.     }
  13.  
  14.     public static int deleteComment (String[] code) {
  15.         String[] finishCode = new String[code.length];
  16.         int startLongComment = 0;
  17.         for (int i = 0; i < code.length; i++) {
  18.             finishCode[i] = "";
  19.             for (int j = 0; j < code[i].length() - 1; j++) {
  20.                 if (startLongComment == 1) {
  21.                     if (code[i].charAt(j) == '*' && code[i].charAt(j + 1) == '/') {
  22.                         j++;
  23.                         startLongComment = 0;
  24.                     }
  25.                 } else {
  26.                     if (code[i].charAt(j) == '/' && code[i].charAt(j + 1) == '*') {
  27.                         startLongComment++;
  28.                     } else if (code[i].charAt(j) == '/' && code[i].charAt(j + 1) == '/') {
  29.                         break;
  30.                     } else {
  31.                         finishCode[i] += code[i].charAt(j);
  32.                     }
  33.                     if (j == code[i].length() - 2 && code[i].charAt(j + 1) != '*') {
  34.                         finishCode[i] += code[i].charAt(j + 1);
  35.                     }
  36.                 }
  37.  
  38.             }
  39.         }
  40.         writeFinishCode(finishCode);
  41.  
  42.         return 1;
  43.     }
  44.  
  45.     public static int writeFinishCode (String[] code) {
  46.         for (int i = 0; i < code.length - 1; i++) {
  47.             if (code[i] == "") {
  48.  
  49.             } else {
  50.                 System.out.print(code[i] + "\n");
  51.             }
  52.         }
  53.         System.out.print(code[code.length - 1]);
  54.         return 1;
  55.     }
  56.  
  57.     public static String[] readLinesFromConsole() {
  58.         Scanner scanner = new Scanner(System.in);
  59.         List<String> lines = new ArrayList<>();
  60.         String line = scanner.nextLine();
  61.         lines.add(line);
  62.         while (scanner.hasNextLine()) {
  63.             String lineLast = line;
  64.             line = scanner.nextLine();
  65.             if ((line == null || line.trim().length() == 0) && (lineLast == null || lineLast.trim().length() == 0))
  66.                 break;
  67.             lines.add(line);
  68.         }
  69.         return lines.toArray(new String[0]);
  70.     }
  71. }
Add Comment
Please, Sign In to add comment