저는 Java와 프로그래밍에 익숙하지 않습니다 ... 사용자 입력의 문자열이있는 곳에서 런 길이 인코딩 프로그램을 구현하는 데 문제가 있습니다. 특정 문자열이 같을 것이다 그래서 KKKKKKKKKKKKKBCCDDDDDDDDDDDDDDDKKKKKMNUUUGGGGG)
는 플래그 문자를 기반으로 인코딩 된 것입니다 그들은 입력 : (예.? $K13BCC$D15$K5MNUUU$G5
이Run-length Encoding Program 문제
누군가가 나에게 무엇이 잘못되었는지 알아내는 데 도움이 수 내 출력으로이 점점 계속 : $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
}
}
}
}
}
귀하의 게시물을 남용하지 마십시오 제공
... 그건 아무것도 학습을위한 도움이되지 않습니다. – px06