解题思路:本题关键是将一个完整的字符串转换为单个字符,以及判断字符是否为数字。
我们可以用max来记录经过的数字长度最大值;count表示数字计数器,当为字母是,重置count=0;end表示数字尾部,每次满足是数字时,对max进行判断,当max<count时,更新max和end。
代码实现:
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();
System.out.println(numberStr(str));
}
}
public static String numberStr(String str){
int max = 0,count = 0,end = 0;
for(int i = 0;i < str.length();i++){
if(str.charAt(i) >= '0' && str.charAt(i) <= '9'){
count++;
if(max < count){
max = count;
end = i;
}
}else{
count = 0;
}
}
return str.substring(end - max + 1,end + 1);
//注意:substring方法是索引从0开始的前闭后开区间的写法,所以是[end - max +1,end+1)
}
}