日期:2014-05-20  浏览次数:20721 次

关于全局变量和局部变量的疑惑
Java code
double pertotalinsure=0.00;//0
            double tmpinsurance=0.00;
            
             //金保无忧险种的份数进行校验,确保为份额的整数倍
            for(int i=0;i<insurelist.size();i++)
            {
                Map insureMap=(Map)insurelist.get(i);
                TMSaleData_Insure tinsure=new TMSaleData_Insure();
                tinsure.fillWithNumberDefaultValue(insureMap,-1);
                
                //TMSaleData_Insure tinsure=(TMSaleData_Insure)insurelist.get(i);
                if(!Util.NVL(tinsure.getPlanid()).equals(""))
                {
                    InsureType itype=this.model.ip.getInsureTypeByCode(tinsure.getPlanid());
                    
                     if(itype!=null)
                    {
                        if(tinsure.getPlanid().equals(model.Insure_JBWY)||tinsure.getPlanid().equals(model.Insure_JBWY_ADD))
                        {
                            isjbwy=true;
                            pertotalinsure+=tinsure.getInsurance()>0?tinsure.getInsurance():0;//1
                            tmpinsurance=Util.toDouble(tdata.getInsurance(), 0.00);
                        }
                    }
                 }
            }
            
            if((tmpinsurance!=pertotalinsure)&&isjbwy&&tmpinsurance>0&&pertotalinsure>0)//2
            {
                ErrString.append(Util.format("[总保费:#0,各险种保费合计:#1,不一致],",
                        Util.NVL(tmpinsurance),Util.NVL(pertotalinsure)));
            }
            

0处,1处和2处的pertotalinsure是一个全局,两个局部吗?那2处的pertotalinsure是1处的结果吗

------解决方案--------------------

LZ的1,2两处都用的是你的全局变量,

只有你在方法体内部重新定义的同名的变量交局部变量