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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ybqred 中级黑马   /  2016-5-6 22:29  /  213 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

private static void getPlus(String a, String b) {
//                1. 获取第一个数的分子和分母。
                String[] tempStr1=a.split("/");
                int top1=Integer.parseInt(tempStr1[0]);
                int bottom1=Integer.parseInt(tempStr1[1]);
                int tempint1=top1/bottom1;
                int tempfloat1=top1%bottom1;
//                2. 获取第二个数的分子和分母
                String[] tempStr2=b.split("/");
                int top2=Integer.parseInt(tempStr2[0]);
                int bottom2=Integer.parseInt(tempStr2[1]);
                int tempint2=top2/bottom2;
                int tempfloat2=top2%bottom2;
//                3. 进行分子分母运算。
                int calBottom=bottom1*bottom2;
                int calTop=(tempint1*calBottom+tempfloat1*bottom2)+(tempint2*calBottom+tempfloat2*bottom1);
//                System.out.println(calTop+"/"+calBottom);
//                4. 对分子分母进行化简
                for(int i=2;i<=bottom1*bottom2;i++){
                        if(calTop%i==0&calBottom%i==0){
                                int finalTop=calTop/i;
                                int finalBottom=calBottom/i;
                                System.out.println(finalTop+"/"+finalBottom);
                                break;
                        }
                        else if(i==bottom1*bottom2){
                                System.out.println(calTop+"/"+calBottom);
                        }
                }
        }

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马