java怎么简化这段代码,

本人新手,怎么简化这段代码,谢谢。

  public void doTask(VehicleOilParam v) {
            if(v.ENGINE_RPM > 0 && v.ENGINE_RPM <= 800){
                if(v.TORQUE_RATE > 0 && v.TORQUE_RATE <= 10){
                    rev_torque_time[0][0] += 1;
                }else if(v.TORQUE_RATE > 10 && v.TORQUE_RATE <= 25){
                    rev_torque_time[0][1] += 1;
                }else if(v.TORQUE_RATE > 25 && v.TORQUE_RATE <= 40){
                    rev_torque_time[0][2] += 1;
                }else if(v.TORQUE_RATE > 40 && v.TORQUE_RATE <= 55){
                    rev_torque_time[0][3] += 1;
                }else if(v.TORQUE_RATE > 55 && v.TORQUE_RATE <= 70){
                    rev_torque_time[0][4] += 1;
                }else if(v.TORQUE_RATE > 70 && v.TORQUE_RATE <= 85){
                    rev_torque_time[0][5] += 1;
                }else if(v.TORQUE_RATE > 85){
                    rev_torque_time[0][6] += 1;
                }
            }else if(v.ENGINE_RPM > 800 && v.ENGINE_RPM <= 1000){
                if(v.TORQUE_RATE > 0 && v.TORQUE_RATE <= 10){
                    rev_torque_time[1][0] += 1;
                }else if(v.TORQUE_RATE > 10 && v.TORQUE_RATE <= 25){
                    rev_torque_time[1][1] += 1;
                }else if(v.TORQUE_RATE > 25 && v.TORQUE_RATE <= 40){
                    rev_torque_time[1][2] += 1;
                }else if(v.TORQUE_RATE > 40 && v.TORQUE_RATE <= 55){
                    rev_torque_time[1][3] += 1;
                }else if(v.TORQUE_RATE > 55 && v.TORQUE_RATE <= 70){
                    rev_torque_time[1][4] += 1;
                }else if(v.TORQUE_RATE > 70 && v.TORQUE_RATE <= 85){
                    rev_torque_time[1][5] += 1;
                }else if(v.TORQUE_RATE > 85){
                    rev_torque_time[1][6] += 1;
                }
            }else if(v.ENGINE_RPM > 1000 && v.ENGINE_RPM <= 1200){
                if(v.TORQUE_RATE > 0 && v.TORQUE_RATE <= 10){
                    rev_torque_time[2][0] += 1;
                }else if(v.TORQUE_RATE > 10 && v.TORQUE_RATE <= 25){
                    rev_torque_time[2][1] += 1;
                }else if(v.TORQUE_RATE > 25 && v.TORQUE_RATE <= 40){
                    rev_torque_time[2][2] += 1;
                }else if(v.TORQUE_RATE > 40 && v.TORQUE_RATE <= 55){
                    rev_torque_time[2][3] += 1;
                }else if(v.TORQUE_RATE > 55 && v.TORQUE_RATE <= 70){
                    rev_torque_time[2][4] += 1;
                }else if(v.TORQUE_RATE > 70 && v.TORQUE_RATE <= 85){
                    rev_torque_time[2][5] += 1;
                }else if(v.TORQUE_RATE > 85){
                    rev_torque_time[2][6] += 1;
                }
            }else if(v.ENGINE_RPM > 1200 && v.ENGINE_RPM <= 1400){
                if(v.TORQUE_RATE > 0 && v.TORQUE_RATE <= 10){
                    rev_torque_time[3][0] += 1;
                }else if(v.TORQUE_RATE > 10 && v.TORQUE_RATE <= 25){
                    rev_torque_time[3][1] += 1;
                }else if(v.TORQUE_RATE > 25 && v.TORQUE_RATE <= 40){
                    rev_torque_time[3][2] += 1;
                }else if(v.TORQUE_RATE > 40 && v.TORQUE_RATE <= 55){
                    rev_torque_time[3][3] += 1;
                }else if(v.TORQUE_RATE > 55 && v.TORQUE_RATE <= 70){
                    rev_torque_time[3][4] += 1;
                }else if(v.TORQUE_RATE > 70 && v.TORQUE_RATE <= 85){
                    rev_torque_time[3][5] += 1;
                }else if(v.TORQUE_RATE > 85){
                    rev_torque_time[3][6] += 1;
                }
            }else if(v.ENGINE_RPM > 1400 && v.ENGINE_RPM <= 1600){
                if(v.TORQUE_RATE > 0 && v.TORQUE_RATE <= 10){
                    rev_torque_time[4][0] += 1;
                }else if(v.TORQUE_RATE > 10 && v.TORQUE_RATE <= 25){
                    rev_torque_time[4][1] += 1;
                }else if(v.TORQUE_RATE > 25 && v.TORQUE_RATE <= 40){
                    rev_torque_time[4][2] += 1;
                }else if(v.TORQUE_RATE > 40 && v.TORQUE_RATE <= 55){
                    rev_torque_time[4][3] += 1;
                }else if(v.TORQUE_RATE > 55 && v.TORQUE_RATE <= 70){
                    rev_torque_time[4][4] += 1;
                }else if(v.TORQUE_RATE > 70 && v.TORQUE_RATE <= 85){
                    rev_torque_time[4][5] += 1;
                }else if(v.TORQUE_RATE > 85){
                    rev_torque_time[4][6] += 1;
                }
            }else if(v.ENGINE_RPM > 1600 && v.ENGINE_RPM <= 1800){
                if(v.TORQUE_RATE > 0 && v.TORQUE_RATE <= 10){
                    rev_torque_time[5][0] += 1;
                }else if(v.TORQUE_RATE > 10 && v.TORQUE_RATE <= 25){
                    rev_torque_time[5][1] += 1;
                }else if(v.TORQUE_RATE > 25 && v.TORQUE_RATE <= 40){
                    rev_torque_time[5][2] += 1;
                }else if(v.TORQUE_RATE > 40 && v.TORQUE_RATE <= 55){
                    rev_torque_time[5][3] += 1;
                }else if(v.TORQUE_RATE > 55 && v.TORQUE_RATE <= 70){
                    rev_torque_time[5][4] += 1;
                }else if(v.TORQUE_RATE > 70 && v.TORQUE_RATE <= 85){
                    rev_torque_time[5][5] += 1;
                }else if(v.TORQUE_RATE > 85){
                    rev_torque_time[5][6] += 1;
                }
            }else if(v.ENGINE_RPM > 1800){
                if(v.TORQUE_RATE > 0 && v.TORQUE_RATE <= 10){
                    rev_torque_time[6][0] += 1;
                }else if(v.TORQUE_RATE > 10 && v.TORQUE_RATE <= 25){
                    rev_torque_time[6][1] += 1;
                }else if(v.TORQUE_RATE > 25 && v.TORQUE_RATE <= 40){
                    rev_torque_time[6][2] += 1;
                }else if(v.TORQUE_RATE > 40 && v.TORQUE_RATE <= 55){
                    rev_torque_time[6][3] += 1;
                }else if(v.TORQUE_RATE > 55 && v.TORQUE_RATE <= 70){
                    rev_torque_time[6][4] += 1;
                }else if(v.TORQUE_RATE > 70 && v.TORQUE_RATE <= 85){
                    rev_torque_time[6][5] += 1;
                }else if(v.TORQUE_RATE > 85){
                    rev_torque_time[6][6] += 1;
                }
            }
    }

6个回答

i = v.ENGINE_RPM-800 <= 0 ? 0:
v.ENGINE_RPM-1200 <= 0 ? 1:
v.ENGINE_RPM-1400 <= 0 ? 2:
v.ENGINE_RPM-1600 <= 0 ? 3:
v.ENGINE_RPM-1800 <= 0 ? 4:
5;
j = v.TORQUE_RATE-10 <= 0 ? 0:
v.TORQUE_RATE-25 <= 0 ? 1:
v.TORQUE_RATE-40 <= 0 ? 2:
v.TORQUE_RATE-55 <= 0 ? 3:
v.TORQUE_RATE-70 <= 0 ? 4:
v.TORQUE_RATE-85 <= 0 ? 5:
6;
rev_torque_time[i][j] += 1;

yxhlfx
双林子木 回复前方一片光明: 。。。都没注意,题主之前的代码看着有点眼花。哈哈
大约 2 年之前 回复
qq_26230421
前方一片光明 你这段代码少了一个1000,总体来说挺赞!!!
大约 2 年之前 回复

public void doTask(VehicleOilParam v) {
int i, j;

    if (v.ENGINE_RPM > 0 && v.ENGINE_RPM <= 800) {
        i = 0;
    } else if (v.ENGINE_RPM > 800 && v.ENGINE_RPM <= 1000) {
        i = 1;
    } else if (v.ENGINE_RPM > 1000 && v.ENGINE_RPM <= 1200) {
        i = 2;
    } else if (v.ENGINE_RPM > 1200 && v.ENGINE_RPM <= 1400) {
        i = 3;
    } else if (v.ENGINE_RPM > 1400 && v.ENGINE_RPM <= 1600) {
        i = 4;
    } else if (v.ENGINE_RPM > 1600 && v.ENGINE_RPM <= 1800) {
        i = 5;
    } else if (v.ENGINE_RPM > 1800) {
        i = 6;
    }

    if (v.TORQUE_RATE > 0 && v.TORQUE_RATE <= 10) {
        j = 0;
    } else if (v.TORQUE_RATE > 10 && v.TORQUE_RATE <= 25) {
        j = 1;
    } else if (v.TORQUE_RATE > 25 && v.TORQUE_RATE <= 40) {
        j = 2;
    } else if (v.TORQUE_RATE > 40 && v.TORQUE_RATE <= 55) {
        j = 3;
    } else if (v.TORQUE_RATE > 55 && v.TORQUE_RATE <= 70) {
        j = 4;
    } else if (v.TORQUE_RATE > 70 && v.TORQUE_RATE <= 85) {
        j = 5;
    } else if (v.TORQUE_RATE > 85) {
        j = 6;
    }

    rev_torque_time[i][j] += 1;
}
fengzhimohan
fengzhimohan 谢谢你
大约 2 年之前 回复

你这个想实现什么功能,看代码太头疼了,

fengzhimohan
fengzhimohan 就是实现多个if else if条件判断。
大约 2 年之前 回复

拆分成两个,if里面的单独拿出来传个index进去,if else转成switch

fengzhimohan
fengzhimohan 谢谢你,问题解决了。
大约 2 年之前 回复

借鉴楼上的

 public void doTask(VehicleOilParam v) {
        int engine_rpm  = v.ENGINE_RPM;
        int torque_rate = v.TORQUE_RATE;
        if(engine_rpm <= 0 || torque_rate <= 0){
            return;
        }
        int i = 0;
        int j = 0;
        i = engine_rpm <= 800  ? 0 :
            engine_rpm <= 1000 ? 1 :
            engine_rpm <= 1200 ? 2 :
            engine_rpm <= 1400 ? 3 :
            engine_rpm <= 1600 ? 4 :
            engine_rpm <= 1800 ? 5 : 6;
        j = torque_rate <= 10 ? 0 :
            torque_rate <= 25 ? 1 :
            torque_rate <= 40 ? 2 :
            torque_rate <= 55 ? 3 :
            torque_rate <= 70 ? 4 :
            torque_rate <= 85 ? 5 : 6;
        rev_torque_time[i][j] += 1;
    }
fengzhimohan
fengzhimohan 谢谢你的回答。
大约 2 年之前 回复

//根据你提供的部分代码,可以使用循环实现简化,思路就是择出特殊步骤,归类相似步骤,笔者精心思考,望采纳
public void doTask(VehicleOilParam v){ //VehicleOilParam这个应该是你没有提供,我这边无法调试

int i,j;                                    
String rev_torque_time[][];                 //由于需要,我在此先声明一个空的二维数组
for(i=0;i<=6;i++){          //两次for循环执行7*7=49次,但是若是给出了v.ENGINE_RPM的值会跳过一些步骤,
    for(j=0;j<=i;j++){
        if(i == 0){         //择出特殊步骤,执行7次
            if(v.ENGINE_RPM > 0 && v.ENGINE_RPM <= 800){
                   if(j == 0){                      //择出特殊步骤,执行1次
                       if(v.TORQUE_RATE > 0 && v.TORQUE_RATE <= 10)
                           rev_torque_time[i][j] += 1;                
                   }else if(j == 6){                        //择出特殊步骤,执行1次
                       if(v.TORQUE_RATE > (15*j-5))
                           rev_torque_time[i][j] += 1; 
                   }else 
                       if(v.TORQUE_RATE > (15*j-5) && v.TORQUE_RATE <= (15*j+10))   //归类相似的步骤,执行5次
                           rev_torque_time[i][j] += 1;                                 
                }
        }else if(i == 6){           //择出特殊步骤,执行7次
            if(v.ENGINE_RPM > (i*200+600)){
                   if(j == 0){              //择出特殊步骤,执行1次
                       if(v.TORQUE_RATE > 0 && v.TORQUE_RATE <= 10)
                           rev_torque_time[i][j] += 1;                
                   }else if(j == 6){                //择出特殊步骤,执行1次
                       if(v.TORQUE_RATE > (15*j-5))
                           rev_torque_time[i][j] += 1; 
                   }else 
                       if(v.TORQUE_RATE > (15*j-5) && v.TORQUE_RATE <= (15*j+10))   //择出特殊步骤,执行5次
                           rev_torque_time[i][j] += 1;                                 
                }
        }else 
            if(v.ENGINE_RPM > (i*200+600) && v.ENGINE_RPM < i*200+800){         //归类相似的步骤,执行5*7=35次
                 if(j == 0){                //择出特殊步骤,执行1次
                       if(v.TORQUE_RATE > 0 && v.TORQUE_RATE <= 10)
                           rev_torque_time[i][j] += 1;                
                   }else if(j == 6){                //择出特殊步骤,执行1次
                       if(v.TORQUE_RATE > (15*j-5))
                           rev_torque_time[i][j] += 1; 
                   }else 
                       if(v.TORQUE_RATE > (15*j-5) && v.TORQUE_RATE <= (15*j+10))   //归类相似的步骤,执行5次
                           rev_torque_time[i][j] += 1;                                 
                }
            }
    }
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问