学习使我头痛 2023-01-26 07:47 采纳率: 100%
浏览 17
已结题

java中的refactor,我要怎么才可以把相似的两段代码合并为一个method


public class ExcelNew {
   //......
    private static String getString(int randStudentSkill, String student, int x, int x1, int x2) {
        if(randStudentSkill <= 5){
            student += (int)(Math.random()*40); //[0,39]
        }else if ((randStudentSkill > 5) && (randStudentSkill <= x)){
            student += ((int)(Math.random()*10) + 40); // [40,49]
        }else if((randStudentSkill > x) && (randStudentSkill <= x1)){
            student += ((int)(Math.random()*20) + 50); // [50,69]
        }else if((randStudentSkill > x1) && (randStudentSkill <= x2)){
            student += ((int)(Math.random()*20) + 70); // [70,89]
        } else{
            student += ((int)(Math.random()*11) + 90); //[90,100]
        }
        student += "\t";
        return student;
    }
    public static void main(String [] args){
       // ...
                if(randStudentSkill <= 7){
                    int randDNSProb = getRandStudentSkill(101);
                    if(randDNSProb <= 5){
                        student += ""; //DNS
                    }else{
                        student += (int)(Math.random()*40); //[0,39]
                    }
                } else if((randStudentSkill > 7) && (randStudentSkill <= 20)){
                        student += ((int)(Math.random()*10) + 40); //[40,49]
                } else if((randStudentSkill > 20) && (randStudentSkill <= 60)){
                    student += ((int)(Math.random()*20) + 50);//[50,69]
                } else if((randStudentSkill > 60) && (randStudentSkill <= 90)){
                    student += ((int)(Math.random()*20) + 70); //[70,89]
                } else{
                    student += ((int)(Math.random()*11) + 90); //[90,100]
                }
            // ...

program里面有好几段相似的代码,我refactor出来了一个method,但是还有一个很相似的,但是我不知道怎么改这个method去包括这个类似的代码。

  • 写回答

1条回答 默认 最新

  • Huazie 全栈领域优质创作者 2023-01-26 09:09
    关注

    参考如下:

    
    public class ExcelNew {
    
       private static String getStudent(int randStudentSkill, String student, int y, int x, int x1, int x2) {
            if ((randStudentSkill > y) && (randStudentSkill <= x)){
                student += ((int)(Math.random()*10) + 40); // [40,49]
            }else if((randStudentSkill > x) && (randStudentSkill <= x1)){
                student += ((int)(Math.random()*20) + 50); // [50,69]
            }else if((randStudentSkill > x1) && (randStudentSkill <= x2)){
                student += ((int)(Math.random()*20) + 70); // [70,89]
            } else{
                student += ((int)(Math.random()*11) + 90); //[90,100]
            }
            return student;
        }
    
       //......
        private static String getString(int randStudentSkill, String student, int x, int x1, int x2) {
            if(randStudentSkill <= 5){
                student += (int)(Math.random()*40); //[0,39]
            }else 
                student += getStudent(randStudentSkill, student, 5, x, x1, x2);
            }
            student += "\t";
            return student;
        }
        public static void main(String [] args){
           // ...
                    if(randStudentSkill <= 7){
                        int randDNSProb = getRandStudentSkill(101);
                        if(randDNSProb <= 5){
                            student += ""; //DNS
                        }else{
                            student += (int)(Math.random()*40); //[0,39]
                        }
                    } else {
                        student += getStudent(randStudentSkill, student, 7, 20, 60, 90);
                    }
          }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 2月3日
  • 已采纳回答 1月26日
  • 修改了问题 1月26日
  • 创建了问题 1月26日

悬赏问题

  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码