我是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
}
}
}
}
}
答案 0 :(得分:1)
首先,您应该在外部counter
块中将else
变量设置为1,因为您在其前面设置了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
}
}
答案 1 :(得分:1)
for循环状态对于初始字符计数和后续字符计数并不完全相同。
使用您的代码,我发现的最简单的修复方法是更改for循环
for (int i = 1; i < inputString.length(); i++) { // start at 1 not 0
在两种情况下都将计数器初始化更改为1而不是0。
int counter = 1; // set counter equal to 1
并且必须更改单个字符输出
for (int j = counter; j > 0; j--) { // for every number in counter
也没有输出最后一个字符。
给
private static void process(String inputString, String flagCharacter) {
// init counter state
char repeatedLetter = inputString.charAt(0);
int counter = 1;
for (int i = 1; i < inputString.length(); i++) {
if (repeatedLetter == (inputString.charAt(i))) {
// match so update counter
counter++;
} else {
if (counter > 3) {
// counter needs to be above 3 to make worthwhile
System.out.print(flagCharacter + repeatedLetter + counter);
} else {
// otherwise we will just output raw
for (int j = counter; j > 0; j--) {
System.out.print(repeatedLetter);
}
}
// and re init our counter
repeatedLetter = inputString.charAt(i);
counter = 1;
}
}
// output last character run
if (counter > 3) {
System.out.print(flagCharacter + repeatedLetter + counter);
} else {
for (int j = counter; j > 0; j--) {
System.out.print(repeatedLetter);
}
}
}