作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 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/
我是一名优秀的程序员,十分优秀!