gpt4 book ai didi

java - 游程编码程序问题

转载 作者:行者123 更新时间:2023-12-02 03:19:56 27 4
gpt4 key购买 nike

我是 Java 和一般编程的新手...我在实现游程编码程序时遇到问题,其中的字符串来自用户输入。 (例如: KKKKKKKKKKKKKBCCDDDDDDDDDDDDDDDKKKKKMNUUUGGGGG) 将根据他们输入的标志字符进行编码。因此该特定字符串将如下所示: $K13BCC$D15$K5MNUUU$G5

有人可以帮我找出问题所在吗?我不断得到这个作为我的输出:$K13BCC$D14$K4MNUUU

我的程序似乎跳过了最后一个字母,并且其中一些字母少了一个。任何帮助将不胜感激!

我为我的困惑代码道歉。我知道这不是那么好。只是想找出这里出了什么问题......

import java.util.Scanner;

public class RunLengthEncoding {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.print("Enter input string: ");
String inputString = input.next();

System.out.print("Enter flag character: ");
String flagCharacter = input.next();

Boolean validInput = false;

for (int i = 0; i < inputString.length(); i++) {
if (Character.isUpperCase(inputString.charAt(i))) {
validInput = true;

} else {
System.out.print("Bad input.");
}

}

char repeatedLetter = inputString.charAt(0);

if (validInput) { // if the input is valid, continue
int counter = 0; // set counter equal to 0
for (int i = 0; i < inputString.length(); i++) { // iterate through the input string

if (repeatedLetter == (inputString.charAt(i))) { // if the current letter is equal to the next
counter++; // increment the counter
repeatedLetter = inputString.charAt(i); // set the repeated letter to the next letter
} else { // if the current letter is not equal to the next letter
if (counter > 3) { // and if the counter is greater than 3
System.out.print(flagCharacter + repeatedLetter + counter); // print the encoded string
} else { // if the counter is not greater than 3
for (int j = counter; j >= 0; j--) { // for every number in counter
System.out.print(repeatedLetter); // print the current repeated letter for as many times as it appears
}

}
repeatedLetter = inputString.charAt(i); // set the new repeated letter
counter = 0; // reset counter to 0

}
}

}

}

}

最佳答案

首先,您应该设置 counter外部变量为 1 else阻止,因为您正在设置 repeatedValue就在它之前,这算作该字母的一次计数。您还需要考虑字符串中的最后一个字符序列,因此您需要最后一个 if当你到达末尾时,在循环之外声明:

int counter = 0; // set counter equal to 0
for (int i = 0; i < inputString.length(); i++) { // iterate through the input string

if (repeatedLetter == (inputString.charAt(i))) { // if the current letter is equal to the next
counter++; // increment the counter
} else { // if the current letter is not equal to the next letter
if (counter > 3) { // and if the counter is greater than 3
System.out.print(flagCharacter + repeatedLetter + counter); // print the encoded string
} else { // if the counter is not greater than 3
for (int j = counter; j > 0; j--) { // for every number in counter
System.out.print(repeatedLetter); // print the current repeated letter for as many times as it appears
}

}
repeatedLetter = inputString.charAt(i); // set the new repeated letter
counter = 1; // reset counter to 1
}
}

// We at the end of the string now, print everything else
if (counter > 3) { // and if the counter is greater than 3
System.out.print(flagCharacter + repeatedLetter + counter); // print the encoded string
} else { // if the counter is not greater than 3
for (int j = counter; j > 0; j--) { // for every number in counter
System.out.print(repeatedLetter); // print the current repeated letter for as many times as it appears
}

}

关于java - 游程编码程序问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39759212/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com