将字符放在数组里面 判断数字存入数组然后计算数组长度
import java.util.Scanner; public class Main { /** * 连续最长数字串 * * @param args */ public static String func(String str) { int max = 0; int length = 0; int end = 0;//用于指示字符串末尾 char[] ch = str.toCharArray(); for (int i = 0; i < ch.length; i++) { if (str.charAt(i) >= '0' && str.charAt(i) <= '9') { length++; if (length > max) { max = length; end = i;//换max值时,end下标需要跟过来 } } else { length = 0; } } return str.substring(end - max + 1, end + 1); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s1 = sc.nextLine(); System.out.println(func(s1)); } }