java实现第一个数字

/*
以下的静态方法实现了:把串 s 中第一个出现的数字的值返回。
如果找不到数字,返回-1
例如:
s = "abc24us43" 则返回 2
s = "82445adb5" 则返回 8
s = "ab" 则返回-1
请分析代码逻辑,并推测划线处的代码。
答案写在 “解答.txt” 文件中
注意:只写划线处应该填的内容,划线前后的内容不要抄写。
*/
public class Demo04 {
public static int getFirstNum(String s) {
if (s == null || s.length() == 0)
return -1;
char c = s.charAt(0);
if (c >= '0' && c <= '9')
return s.charAt(0)-'0'; // 填空
return getFirstNum(s.substring(1)); // 填空
}
public static void main(String[] args) {
String s1 = "abc24us43"; //则返回2
String s2 = "82445adb5"; //则返回8
String s3 = "ab"; //则返回-1 
System.out.println(getFirstNum(s1));
System.out.println(getFirstNum(s2));
System.out.println(getFirstNum(s3));
}
}
运行结果:
2
8
-1
原文地址:https://www.cnblogs.com/a1439775520/p/13076417.html