该程序应该给出最长的升序。
因此,例如,如果我输入“12134707”,它应该给出 output = 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;
}
}