zoukankan      html  css  js  c++  java
  • 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
    
  • 相关阅读:
    Python3输入输出
    Python3文件
    Python3OS文件/方法
    makefile通用版本(一)
    C语言正则表达式
    正则表达式
    sed、awk工具
    shell编程
    Sqlite3-安装使用
    Powershell-获取命令和帮助
  • 原文地址:https://www.cnblogs.com/a1439775520/p/12947218.html
Copyright © 2011-2022 走看看