A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© wenjd0000 中级黑马   /  2014-8-28 19:24  /  906 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

        public static void main(String[] args) {
                method("123");
                method("1x3");
                method("0.23");
        }
       
        private static boolean method(String s){
                boolean b = false;
                int i;
                try{
                        int x =Integer.valueOf(s);
                        i = x;
                }catch (Exception e){
                        System.out.println("这是什么啊?");
                        return b;
                }
                        System.out.println("success");
                        System.out.println(i+1);
                        b = true ;
                        return b;
        }

把一个String转化为int类型的,用try catch来操作
实际开发这么写可以吗?

4 个回复

倒序浏览
可以,写少点,维护起来费劲
private static boolean method(String s){
              try{
                        int x =Integer.valueOf(s);
                       return true;
                }catch (Exception e){
                        System.out.println("这是什么啊?");
                        return false;
                }
        }

回复 使用道具 举报
没有什么可以不可以的,只有好与不好,当然啦,用尽量少的代码实现相同的功能,并保证程序的健壮性、优越性就更好了
回复 使用道具 举报 1 0
路过 学习一下
回复 使用道具 举报
{:2_31:}{:2_31:}{:2_31:}{:2_31:}{:2_31:}{:2_31:}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马