java在for循环中怎样修改参数值?

        // 待认证
        QueryFilter queryFilter0 = new QueryFilter();
        queryFilter0.addCommand(new Command("busStatus", Op.EQ, "0"));
        int taxStatus0 = taxInfoDao.getCount(queryFilter0);
        int businessStatus0 = businessInfoDao.getCount(queryFilter0);
        int socialSecurityStatus0 = socialSecurityDao.getCount(queryFilter0);
        int status0 = taxStatus0 + businessStatus0 + socialSecurityStatus0;
        // 待审核
        QueryFilter queryFilter1 = new QueryFilter();
        queryFilter1.addCommand(new Command("busStatus", Op.EQ, "1"));
        int taxStatus1 = taxInfoDao.getCount(queryFilter1);
        int businessStatus1 = businessInfoDao.getCount(queryFilter1);
        int socialSecurityStatus1 = socialSecurityDao.getCount(queryFilter1);
        int status1 = taxStatus1 + businessStatus1 + socialSecurityStatus1;
        // 已通过
        QueryFilter queryFilter2 = new QueryFilter();
        queryFilter2.addCommand(new Command("busStatus", Op.EQ, "2"));
        int taxStatus2 = taxInfoDao.getCount(queryFilter2);
        int businessStatus2 = businessInfoDao.getCount(queryFilter2);
        int socialSecurityStatus2 = socialSecurityDao.getCount(queryFilter2);
        int status2 = taxStatus2 + businessStatus2 + socialSecurityStatus2;
        // 未通过
        QueryFilter queryFilter3 = new QueryFilter();
        queryFilter3.addCommand(new Command("busStatus", Op.EQ, "3"));
        int taxStatus3 = taxInfoDao.getCount(queryFilter3);
        int businessStatus3 = businessInfoDao.getCount(queryFilter3);
        int socialSecurityStatus3 = socialSecurityDao.getCount(queryFilter3);
        int status3 = taxStatus3 + businessStatus3 + socialSecurityStatus3;

这样的代码从status0到status3有4组
java有没有这种功能,写一个函数,里面用for循环

  JSONObject result = new JSONObject();
  for(int i = 0; i < 4 ;i++){
    QueryFilter queryFilteri = new QueryFilter();
    queryFilteri.addCommand(new Command("busStatus", Op.EQ, "i"));
    int taxStatusi = taxInfoDao.getCount(queryFilteri);
    int businessStatusi = businessInfoDao.getCount(queryFilteri);
    int socialSecurityStatusi = socialSecurityDao.getCount(queryFilteri);
    int result = taxStatusi + businessStatusi + socialSecurityStatusi;
    result.put("statusi", statusi);
   }
   

当然我这样写是错的,但是有没有什么功能能实现这个函数?

阅读 2.5k
2 个回答

这难道不应该用数组去实现吗?恭喜你,看到一坨屎一样的代码想要把它优化是成长为一名高级工程师的必经之路,你看那些大师们身上都是屎迹斑斑。

QueryFilter[] queryFilter = new QueryFilter[4];
int[] taxStatus = new int[4];
int[] businessStatus = new int[4];
int[] socialSecurityStatus = new int[4];
int[] status = new int[4];

for(int i = 0; i < 4; i++) {
    queryFilter[i] = new QueryFilter();
    queryFilter[i].addCommand(new Command("busStatus", Op.EQ, "" + i));
    taxStatus[i] = taxInfoDao.getCount(queryFilter[i]);
    businessStatus[i] = businessInfoDao.getCount(queryFilter[i]);
    socialSecurityStatus[i] = socialSecurityDao.getCount(queryFilter[i]);
    status[i] = taxStatus[i] + businessStatus[i] + socialSecurityStatus[i];
}

只需稍加改动:

JSONObject result = new JSONObject();
  for(int i = 0; i < 4 ;i++){
    QueryFilter queryFilteri = new QueryFilter();
    queryFilteri.addCommand(new Command("busStatus", Op.EQ, i+""));
    int taxStatusi = taxInfoDao.getCount(queryFilteri);
    int businessStatusi = businessInfoDao.getCount(queryFilteri);
    int socialSecurityStatusi = socialSecurityDao.getCount(queryFilteri);
    int result = taxStatusi + businessStatusi + socialSecurityStatusi;
    result.put("status"+i, statusi);
   }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题