写一个方法method1(String s),返回字符串参数中最后一个数字。例:“His telephone number is 123456789” return 123456789;
     "There are 1 bees,2 butterflies,3 dragonflies" return 3;

解决方案 »

  1.   

    1:通过正则表达式找出字符串中的数字s1
    2:s1.substring(s1.length()-1,s1.length())
      

  2.   

    CSDN 不是帮人写作业的场所!
      

  3.   

    楼主 以后 要是搞编程考试的时候 别忘了带个笔记本上去 记住在csdn里面发一下 不到一分钟答案就出来了 呵呵 
      

  4.   

    用split把字符串到数组里,再从后到前判断是不是数字,是数字就输出
      

  5.   

    你好,一下是代码,记得给我点分
    public class TestInterceptString {
    public static void main(String[] args) {
    String str = "There are 1 bees,2 butterflies,3 dragonflies";
    str = method(str);
    System.out.println(str); } public static String method(String str) {
    String[] temps = str.split("[^0-9]");
    str = temps[temps.length - 1];
    return str;
    }
    }
      

  6.   

    public class StringIntercept {
    public static void main(String[] args){
    String s1="His telephone number is 123456789"; 
        String s2="There are 1 bees,2 butterflies,3 dragonflies";
        String s3 = "";
        StringIntercept si = new StringIntercept();
        s3 = si.method(s1);
        System.out.println(s3);
        s3 = si.method(s2);
        System.out.println(s3);
    }

     public static String method(String str){

    String[] s;
    String length = "";
    s = str.split(",");
    length = s[s.length - 1]; if(str == length){
    str = s[s.length - 1];
    s = str.split(" ");
    }

    str = s[s.length - 1];
    s = str.split("[^0-9]");
    str = s[s.length - 1];

    return str;
    }
    }