读入一个字符串str,输出字符串str中的连续最长的数字串
输入描述:测试输入包含1个测试用例,一个字符串str,长度不超过255。
输出描述:在一行内输出str中里连续最长的数字串。
示例 输入:abcd12345ed125ss123456789 输出:123456789
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String str = sc.nextLine();
int max = 0; //记录长度最大值
int count = 0; //计数
int end = 0; //结束的下标
for(int i=0;i<str.length();i++){ //遍历一遍 若为数字则计数加一
if(str.charAt(i)>='0'&&str.charAt(i)<='9'){
count++;
if(max<count){ //若比记录里面的max值大 则替换max和相应的end值
max = count;
end = i;
}
}else{
count = 0;
}
}
System.out.println(str.substring(end-max+1,end+1));
//substring方法 检索开始位置是end-max+1 结束位置为end+1
}
}
}