프로그램은 가장 긴 오름차순을 제공합니다.문자열에서 가장 긴 오름차순 숫자 시퀀스 찾기
예를 들어, "12134707"을 입력하면 출력 = 1347이되지만 12를주는 것은 분명히 가장 길지 않습니다. 나는 무엇을 놓치고 잘못 했는가?
import java.util.Scanner;
public class Ascending
{
public static void main(String args[]){
System.out.print("Enter a number = ");
Scanner in = new Scanner(System.in);
String n = in.nextLine();
//print soluton
System.out.println("output = " + itsAscending(n));
}
public static String itsAscending(String n) {
int length = n.length();
String maxlongest = "";
for(int i = 1; i < length; i++) {
char first = n.charAt(i-1);
char second = n.charAt(i);
char next = n.charAt(i+1);
int f = (int)(first - 48);
int s = (int)(second - 48);
int nx = (int)(next - 48);
String longest = "";
int max = 0;
//test to find the ascending order
if (f<s){
longest = longest + f;
if(n.length()>2){
if(f<s){
longest = longest + s;
length = longest.length();
i++;
}
else{
i++;
continue;
}
}
//test to find the longest string
for (i=1; i<length; i++){
if (length > max){
max = length;
maxlongest = longest;
return maxlongest;
}
else {
return maxlongest;
}
}
}
}
return maxlongest;
}
}
숙제에 문제가 있습니까? – Anubhab
나는 무엇이 잘못되었는지 그리고 어디에 있는지 찾아 내기 위해 코드를 디버깅 할 것을 제안한다. –
@Anubhab가 중요합니까? 그것은 나에게 그가 노력을 기울 였고 틀린 한 가지를 찾을 수없는 것 같습니다. 그럼 왜 그가 여기 묻지 않았을까요? – Joetjah