使用java语言,将字符串中连续重复出现的字母变成“出现次数“+字母的格式

import java.util.Scanner;
 
 
public class Robot {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String string=sc.nextLine();
		getNumFromString(string);
	}
 
	private static void getNumFromString(String string) {
		// TODO Auto-generated method stub
		StringBuilder sb= new StringBuilder();
		char  c= string.charAt(0);
		int count=1;
		for (int i = 1; i < string.length(); i++) {
			char s= string.charAt(i);
			if (s==c) {
				count++;
			}
			else {
				
				if (count>1) {
				sb.append(count);
				sb.append(c);
				count=1;
				}else{
				sb.append(c);
				}
			}
			c=s;
		}
        if (count>1) {
			sb.append(count);
		}
		sb.append(c);
		System.out.println(sb.toString());
		
	}
}

你可能感兴趣的:(java,开发语言)