저는 Java 기술에 녹초가 있습니다.하지만 사용자에게 문자열을 입력하라는 메시지를 표시하고 순서가 지정된 하위 시퀀스를 늘리는 최대 길이를 표시하는 프로그램을 작성하려고했습니다. 예를 들어, 사용자가 Welcome
을 입력하면 프로그램은 Welo
을 출력합니다. 사용자가 WWWWelllcommmeee
을 입력하면 프로그램은 여전히 Welo
을 출력합니다. 나는이 일을 많이 해왔지만 그것이해야하는 일을하지 않고 있으며 솔직히 왜 그랬는지에 관해서는 분실하고 있습니다.문자열에서 가장 길게 증가하는 서브 시퀀스를 어떻게 얻을 수 있습니까?
import java.util.ArrayList;
import java.util.Scanner;
public class Stuff {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a string. ");
String userString = input.next();
ArrayList charList = new ArrayList();
ArrayList finalList = new ArrayList();
int currentLength = 0;
int max = 0;
for(int i = 0; i < userString.length(); i++){
charList.add(userString.charAt(i));
for(int j = i; j < userString.length(); j++){
int k=j+1;
if(k < userString.length() && userString.charAt(k) > userString.charAt(j)){
charList.add(userString.charAt(j));
currentLength++;
}
}
}
if(max < currentLength){
max = currentLength;
finalList.addAll(charList);
}
for (int i = 0; i < finalList.size(); i++){
char item = (char) finalList.get(i);
System.out.print(item);
}
int size1 = charList.size();
int size2 = finalList.size();
System.out.println("");
System.out.println("Size 1 is: " + size1 + " Size 2 is : " + size2);
}
}
내 코드, 만약 I Welcome
입력, 출력 WWeceeclcccome
.
누군가 내가 뭘 잘못하고 있는지에 대한 조언이 있습니까?
오류를 찾기 위해 디버깅하는 방법을 배워야합니다. IDE에 통합 된 디버그 기능을 사용하거나 이전이지만 강력한 "system.out.print"를 사용하여 코드가 잘못된 부분을 기록하고 기록하십시오. 행운을 빕니다. –