我正在尝试创建一个程序,该程序将从给定的数字输入中找到最长的升序子字符串。
(例如输入=“1215678”输出=“5678”)
循环底部的返回不起作用:它说它在底部的返回中找不到 asc 的变量....怎么了?
import java.util.Scanner;
public class Ascending
{
public static void main(String[] args)
{
System.out.print("Enter a number = ");
//scan the input
Scanner in = new Scanner(System.in);
String k = in.nextLine();
}
public static String itsAscending(String n)
{
int length = k.length();
for(int i = 0; i < length; i++)
{
char first = k.charAt(i);
char next = k.charAt(i+1);
int f = (int)(first - 48);
int nx = (int)(next - 48);
String asc;
String works = "";
if (f<nx)
{
works = works + k.substring(i, i+2);
asc = works.substring(0, works.length()-1);
}
else
{
works = works + "";
asc = works.substring(i, works.length()-1);
}
}
return asc;
}
}