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去包括这个类似的代码。

参考如下:


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);
                }
      }
}