目前正在学习 Java,对这个相当基本的 RLE 程序有困难。我认为错误与重置字符数有关吗?它似乎正在输出应该被移走的以前的值。
请帮忙!我觉得自己太笨了,我不知道如何解决它哈哈
例如格式化: AABCEDDDGHIIIIII 将被转换为 A2B1C1E1D3G1H1I5
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter string for encoding: ");
String uString = input.next();
int count = 1;
String eString = "";
for (int i = 0; i < uString.length(); i++){
if ((i != 0) && (uString.charAt(i) == uString.charAt(i-1))){
count++;
}else if(i!= 0){
eString = eString + uString.charAt(i) + String.valueOf(count);
count = 1;
}
System.out.println(String.valueOf(uString.charAt(i)) + String.valueOf(i) + String.valueOf(count)); //just to see what's happening in my code
}System.out.println("Unencoded string's length: " + uString.length() + "\nEncoded string:" + eString + ", encoded string's length: " + eString.length());
}
}```