Java 游程编码压缩

Java 游程编码压缩,java,Java,因此,这段代码的要点是压缩字符串,如果字符串出现超过3次,则添加“#+出现次数”。对于字母,格式为(#num)(字母),对于数字值,它们都有“#” 例如,如果我有yyy77777,输出将是#4y#5#7 但我的问题是它没有打印前面带有“#”的数值,所以我的输出是$4y#57 我的另一个问题是,当它小于3时,它只打印相同的内容。因此,也就是说:nnn ff的输出应为#5nff 但我的代码把它变成了#5n2f 任何帮助都将不胜感激 public class Compress { public st

因此,这段代码的要点是压缩字符串,如果字符串出现超过3次,则添加“#+出现次数”。对于字母,格式为(#num)(字母),对于数字值,它们都有“#”

例如,如果我有yyy77777,输出将是#4y#5#7 但我的问题是它没有打印前面带有“#”的数值,所以我的输出是$4y#57

我的另一个问题是,当它小于3时,它只打印相同的内容。因此,也就是说:nnn ff的输出应为#5nff 但我的代码把它变成了#5n2f

任何帮助都将不胜感激

public class Compress {

public static void main(String[] args) {
    java.util.Scanner in = new java.util.Scanner(System.in);
    String s = in.next();
    String compressString = compress(s);
    System.out.println(compressString);

}
public static String compress (String original){
    String str = "";
   // int n = (Integer) null;
    int count = 1;
    for(int i=0; i<original.length()-1; i++){

        if(original.charAt(i) == original.charAt(i+1)){
            count++;
            if(i == original.length()-2){
                if(count == 1){
                    str = str + original.charAt(i);
                }
                //if count is equal or greater than 3, print with #
                else if (count >= 3) {
                    str = str + "#" + count +  original.charAt(i);
                }          
                else{
                    str = str + original.charAt(i);
                }                    
            }
        }
        else{
            if(count == 1){
                if(i == original.length() - 2){
                    str = str + original.charAt(i);
                    str = str + original.charAt(i+1);
                }
                else{
                str = str + original.charAt(i);
                }
            }
            else{
                if(i == original.length() - 2){
                    str = str + count + original.charAt(i);
                    str = str + original.charAt(i+1);
                }
                else{
                    str = str +"#"+ count + original.charAt(i);
                }


            }

            count = 1;
        }
    }
    return str;
} 
公共类压缩{
公共静态void main(字符串[]args){
java.util.Scanner in=new java.util.Scanner(System.in);
字符串s=in.next();
字符串压缩字符串=压缩;
System.out.println(压缩字符串);
}
公共静态字符串压缩(字符串原始){
字符串str=“”;
//int n=(整数)null;
整数计数=1;
对于(int i=0;i=3){
str=str+“#”+计数+原始字符(i);
}          
否则{
str=str+原始字符(i);
}                    
}
}
否则{
如果(计数=1){
如果(i==original.length()-2){
str=str+原始字符(i);
str=str+original.charAt(i+1);
}
否则{
str=str+原始字符(i);
}
}
否则{
如果(i==original.length()-2){
str=str+count+original.charAt(一);
str=str+original.charAt(i+1);
}
否则{
str=str+“#”+计数+原始字符(i);
}
}
计数=1;
}
}
返回str;
} 

}

您的代码当前以完全相同的方式处理数字和非数字。要修复代码,只需添加if语句来检查字符是否为数字

您可以在将
#(count)(character)
附加到结果之前添加if语句

替换这一行(有两行是这样的!两行都替换):

以下是我将如何解决此问题:

public static String compress (String original){
    String str = "";
    int count = 0;
    char currentChar = original.charAt(0);
    for(int i=0; i<original.length(); i++){
        if (currentChar == original.charAt(i)) { // count it if this not a new character
            currentChar = original.charAt(i);
            count++;
        } else { // if we have encountered a new character
            if (count > 2) { // and we have already counted 2 or more of the previous char
                if (Character.isDigit(currentChar)) {
                    str += "#" + count + "#" + currentChar;
                } else {
                    str += "#" + count + currentChar;
                }
            } else if (count == 2) { // if we have only counted two of the previous char
                str += currentChar;
                str += currentChar;
            } else if (count == 1) { // if we have only counted one of the previous char
                str += currentChar;
            }
            currentChar = original.charAt(i);
            count = 1;
        }
    }
    // treat the end of the string as a new char
    if (count > 2) {
        if (Character.isDigit(currentChar)) {
            str += "#" + count + "#" + currentChar;
        } else {
            str += "#" + count + currentChar;
        }
    } else if (count == 2) {
        str += currentChar;
        str += currentChar;
    } else if (count == 1) {
        str += currentChar;
    }
    return str;
}
公共静态字符串压缩(字符串原始){
字符串str=“”;
整数计数=0;
char currentChar=原始字符(0);
对于(inti=0;i2){//我们已经计算了2个或更多的前一个字符
if(字符.isDigit(currentChar)){
str+=“#”+计数+“#”+当前字符;
}否则{
str+=“#”+count+currentChar;
}
}else如果(count==2){//如果我们只计算了前面的两个字符
str+=currentChar;
str+=currentChar;
}else如果(count==1){//如果我们只计算了前面的一个字符
str+=currentChar;
}
currentChar=原始字符(i);
计数=1;
}
}
//将字符串结尾视为新字符
如果(计数>2){
if(字符.isDigit(currentChar)){
str+=“#”+计数+“#”+当前字符;
}否则{
str+=“#”+count+currentChar;
}
}否则如果(计数=2){
str+=currentChar;
str+=currentChar;
}否则如果(计数=1){
str+=currentChar;
}
返回str;
}

我完全忘记了“.isDigit()”函数,非常感谢!!我仍然有一点困难,不知道如何保持一个字符串,即“abbc”输出“abbc”。现在,我的代码输出“a2bc”,我认为这与嵌套的if/else循环有关,但我似乎无法理解out@MarthaN. 我无法真正理解您的代码,但我已编辑以包含我自己的解决方案。有帮助吗?是的!另一个快速添加-如果我想让我的输出说,有3行,即:bbbb888(新行)jknnn(新行)0003nn,我将如何打印它?@MarthaN。请注意,不鼓励在回答完问题后删掉整个问题。这对未来的游客不利。有人已经回滚了编辑。你的补充问题应该在新的帖子中提出。请先在谷歌上搜索一下,然后再回来发布另一个问题。你的问题目前还不清楚。你所说的“我该如何印刷它”是什么意思?您不能使用
System.out.println
?在发布新问题时,一定要详细描述你遇到的问题。嗨,玛莎!你应该让你的问题像清洁工回答它时一样,这样将来它可以帮助其他人。你在他/她的答案上打上的“勾号”足以表明答案是有帮助的,并且解决了你的问题。
if (Character.isDigit(original.charAt(i))) {
    str = str + "#" + count + "#" + original.charAt(i);
} else {
    str = str + "#" + count +  original.charAt(i);
}
public static String compress (String original){
    String str = "";
    int count = 0;
    char currentChar = original.charAt(0);
    for(int i=0; i<original.length(); i++){
        if (currentChar == original.charAt(i)) { // count it if this not a new character
            currentChar = original.charAt(i);
            count++;
        } else { // if we have encountered a new character
            if (count > 2) { // and we have already counted 2 or more of the previous char
                if (Character.isDigit(currentChar)) {
                    str += "#" + count + "#" + currentChar;
                } else {
                    str += "#" + count + currentChar;
                }
            } else if (count == 2) { // if we have only counted two of the previous char
                str += currentChar;
                str += currentChar;
            } else if (count == 1) { // if we have only counted one of the previous char
                str += currentChar;
            }
            currentChar = original.charAt(i);
            count = 1;
        }
    }
    // treat the end of the string as a new char
    if (count > 2) {
        if (Character.isDigit(currentChar)) {
            str += "#" + count + "#" + currentChar;
        } else {
            str += "#" + count + currentChar;
        }
    } else if (count == 2) {
        str += currentChar;
        str += currentChar;
    } else if (count == 1) {
        str += currentChar;
    }
    return str;
}