1 package com.review; 2 3 import java.util.Scanner; 4 5 /** 6 * @program: com.review 7 * @description: 8 * @author: Mr.Lin 9 * @create: 2019年8月13日 10 **/ 11 public class Compress03 { 12 static Scanner sc= new Scanner(System.in); 13 public static void main(String[] args) { 14 System.out.println("输入:"); 15 String cluster = sc.next(); 16 17 String s1 = cluster + " "; 18 String s3 = ""; 19 20 int index; 21 do { 22 index = 0; 23 for(int i=0;i<s1.length();i++) { 24 if(s1.charAt(i)!=s1.charAt(i+1)) { 25 index = i+1; 26 break; 27 } 28 } 29 String s2 = s1.substring(0, index); 30 s3 += s2.charAt(0) + "" + ((s2.length()-1) == 0 ? "" : s2.length()); 31 s1 = s1.substring(index, s1.length()); 32 }while(s1.length()>1); 33 System.out.println("输出:\n"+s3); 34 } 35 36 }