Java中字符串经典小例题,字符串中数字之和

例题: String str = "ad45kl3mjf903f"; 求出45+3+903=951

提醒:int num = Integer.parseInt(str);

废话不多说,直接上代码

方法一:

public class Test01 {
     public static   void  reCode(String w){
         String result="";
         String numstr="";
         int sum=0;
         for (int i = 0; i <w.length() ; i++) {
             char ch1=w.charAt(i);
             //判断字符串第一个是否是数字
             if (ch1>='0'&&ch1<='9'){
                 numstr+=ch1;
                 //判断i的下一位是否字母
                 if (w.charAt(i+1)<'0'||w.charAt(i+1)>'9'){
                     //拼接结果
                     result+=numstr+"+";
                      //转换数字
                     int num = Integer.parseInt(numstr);
                      //记录结果
                      sum+=num;
                     //截取完成一个数字,切记要清空numstr
                     numstr="";   
                 }
             }
         }
        result=result.substring(0,result.length()-1);
        System.out.println(result+"="+sum);
     }
    public static void main(String[] args) {
       //String str = "ad45kl3mjf903f";
       // 求出45+3+903=951
       //int num = Integer.parseInt(str);
        Scanner in =new  Scanner(System.in);
        System.out.println("请输入字符串:");
        String s=in.nextLine();
        char ch0= s.charAt(s.length()-1);
        //判断最后最后一个字符是否是数字
        if (ch0>='0'&&ch0<='9'){
            //是数字,就添加字符
            s+="b";
            reCode(s);
        }else {
            //不是数字就直接调用方法
           reCode(s);
        }

    }
}

方法二:

public class Test01 {
    public static  void  reCode(String w){
        String result="";
        String numstr="";
        int sum=0;
        for (int i = 0; i <w.length() ; i++) {
            //判断第一个出现的数字
            char ch1=w.charAt(i);
            if (ch1>='0'&&ch1<='9'){
                //截取子串
                String ss=w.substring(i);
                for (int j = 0; j <ss.length() ; j++) {
                    //判断子串的第一个字母出现位置
                   char ch2 =ss.charAt(j);
                   if (ch2<'0'||ch2>'9'){
                       numstr=ss.substring(0,j);
                       int num = Integer.parseInt(numstr);
                       result+=numstr+"+";
                       sum+=num;
                       i+=j;
                       break;
                   }
                }
            }
        }
        result=result.substring(0,result.length()-1);
        System.out.println(result+"="+sum);
    }
    public static void main(String[] args) {
       //String str = "ad45kl3mjf903f";
       // 求出45+3+903=951
       //int num = Integer.parseInt(str);
        Scanner in =new  Scanner(System.in);
        System.out.println("请输入字符串:");
        String s=in.nextLine();
        char ch0= s.charAt(s.length()-1);
        //判断最后最后一个字符是否是数字
        if(ch0>='0'&&ch0<='9'){
            s+="b";
            reCode(s);
        }else {
            reCode(s);
        }
    }
}

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值