问题描述
题目的地址http://hero.csdn.net/Question/Details?ID=633&ExamID=628&from=4我的代码packagecom.common.entrance;//请不要使用package,这将会导致您的代码不能通过测试importjava.io.InputStream;importjava.math.BigDecimal;importjava.text.DecimalFormat;importjava.text.NumberFormat;importjava.util.HashMap;importjava.util.Iterator;importjava.util.Map;importjava.util.Set;importjava.util.Scanner;classData{//-1表示未初始化或者数据丢失publicstaticfloatBABNUMBER=-1;privateintid;privatefloatincome=0.00f;privatefloatexpenditure=0.00f;privatefloatbalance=0.00f;publicfloatformat(floatnumber){number=newBigDecimal(number).setScale(2,BigDecimal.ROUND_HALF_UP).floatValue();returnnumber;}publicstaticStringformatNumber(floatnumber){DecimalFormatdf;if(number==0){df=newDecimalFormat("0.00");}else{df=newDecimalFormat("#.00");}Stringnum=df.format(number);returnnum;}publicData(intid,floatincome,floatexpenditure,floatbalance){super();this.id=id;this.income=format(income);this.expenditure=format(expenditure);this.balance=format(balance);}publicData(){super();//TODOAuto-generatedconstructorstub}/***检测是否为完整数据**@return*/publicbooleancheckIntegrity(){if(income==-1||expenditure==-1||balance==-1){returnfalse;}returntrue;}publicintgetId(){returnid;}publicfloatgetIncome(){returnincome;}publicvoidsetIncome(floatincome){this.income=format(income);}publicfloatgetExpenditure(){returnexpenditure;}publicvoidsetExpenditure(floatexpenditure){this.expenditure=format(expenditure);}publicfloatgetBalance(){returnbalance;}publicvoidsetBalance(floatbalance){this.balance=format(balance);}}classHandleData{//扫描修复的次数privatestaticintrepairTime=3;//用户数据privatestaticMap<Integer,Data>dataMap;//用于存储待修复的数据privatestaticMap<Integer,Data>toBeRapairedMap=newHashMap<Integer,Data>();/***修复数据**@paramid*/privatestaticvoidrepairDataById(intid){Datadata=dataMap.get(id);if(!data.checkIntegrity()){//修复收入if(data.getIncome()==Data.BABNUMBER){if(data.getExpenditure()!=0&&data.getExpenditure()!=Data.BABNUMBER){data.setIncome(0);dataMap.put(id,data);}elseif(dataMap.get(id-1)!=null&&dataMap.get(id-1).checkIntegrity()&&data.getBalance()!=Data.BABNUMBER){floatincome=data.getBalance()+data.getExpenditure()-dataMap.get(id-1).getBalance();data.setIncome(income);dataMap.put(id,data);}elseif(dataMap.get(id-1)==null&&data.getBalance()!=Data.BABNUMBER){floatincome=data.getBalance()+data.getExpenditure();data.setIncome(income);dataMap.put(id,data);}}//修复支出if(data.getExpenditure()==Data.BABNUMBER){if(data.getIncome()!=0&&data.getIncome()!=Data.BABNUMBER){data.setExpenditure(0.00f);dataMap.put(id,data);}elseif(dataMap.get(id-1)!=null&&dataMap.get(id-1).checkIntegrity()&&data.getBalance()!=Data.BABNUMBER){floatexpenditure=data.getIncome()-data.getBalance()+dataMap.get(id-1).getBalance();data.setExpenditure(expenditure);dataMap.put(id,data);}elseif(dataMap.get(id-1)==null&&data.getBalance()!=Data.BABNUMBER){floatexpenditure=data.getIncome()-data.getBalance();data.setExpenditure(expenditure);dataMap.put(id,data);}}//修复余额if(data.getBalance()==Data.BABNUMBER){if(dataMap.get(id-1)!=null&&dataMap.get(id-1).checkIntegrity()&&data.getIncome()!=Data.BABNUMBER&&data.getExpenditure()!=Data.BABNUMBER){floatbalance=data.getIncome()-data.getExpenditure()+dataMap.get(id-1).getBalance();data.setBalance(balance);dataMap.put(id,data);}elseif(dataMap.get(id+1)!=null&&dataMap.get(id+1).checkIntegrity()){floatbalance=dataMap.get(id+1).getExpenditure()-dataMap.get(id+1).getIncome()+dataMap.get(id+1).getBalance();data.setBalance(balance);dataMap.put(id,data);}elseif(dataMap.get(id-1)==null){floatbalance=data.getIncome()-data.getExpenditure();data.setBalance(balance);dataMap.put(id,data);}}}}/***检测不完整数据并存储*/privatestaticvoidcheckIntegrityData(){Set<Integer>key=dataMap.keySet();Iterator<Integer>it=key.iterator();while(it.hasNext()){intid=it.next();Datadata=dataMap.get(id);if(!data.checkIntegrity()){toBeRapairedMap.put(id,data);}}}privatestaticvoidrepairData(){Set<Integer>rkey=toBeRapairedMap.keySet();Iterator<Integer>rit=rkey.iterator();while(rit.hasNext()){intid=rit.next();repairDataById(id);if(!dataMap.get(id).checkIntegrity()){//System.out.println("error:id="+id+"的数据无法修复!!!");}}toBeRapairedMap.clear();}/***修复数据的接口**@paramuserDataMap*@return*/publicstaticMap<Integer,Data>repair(Map<Integer,Data>userDataMap){if(userDataMap==null){returnnull;}dataMap=userDataMap;//检测不完整数据并存储checkIntegrityData();//修复数据while(toBeRapairedMap.size()>0){repairTime=repairTime-1;repairData();checkIntegrityData();if(repairTime<0){break;}}returndataMap;}publicstaticStringparseInput(InputStreaminput){Scannerscanner=newScanner(input);Stringd="";try{scanner.useDelimiter("n");//System.getProperty("line.separator")==nStringBufferbuffer=newStringBuffer();while(scanner.hasNext()){Stringnext=scanner.next();if(next.trim().equals("")||next==null){//System.out.println("---------over----------");input.close();scanner.close();break;}buffer.append(next);}d=buffer.toString().replaceAll("\?","-1");}catch(Exceptione){System.out.println(e);}returnd;}publicstaticMap<Integer,Data>parseDataStr(StringdataStr){//可以修改usedelimiter参数以读取不同分隔符分隔的内容Map<Integer,Data>dataMap=newHashMap<Integer,Data>();if(dataStr.equals("")||dataStr==null){returndataMap;}String[]lines=dataStr.split("r");for(Stringline:lines){//System.out.println(line);Datadata=null;String[]lineData=line.split(";");if(lineData.length<4){break;}intid=Integer.parseInt(lineData[0]);floatincome=Float.parseFloat(lineData[1]);floatexpenditure=Float.parseFloat(lineData[2]);floatbalance=Float.parseFloat(lineData[3]);data=newData(id,income,expenditure,balance);dataMap.put(id,data);}returndataMap;}}publicclassEntrance{privatestaticMap<Integer,Data>dataMap=newHashMap<Integer,Data>();publicstaticvoidmain(String[]args){Map<Integer,Data>dataMap=newHashMap<Integer,Data>();System.out.println("流水记录ID;收入;支出;余额");StringdataStr=HandleData.parseInput(System.in);dataMap=HandleData.parseDataStr(dataStr);dataMap=HandleData.repair(dataMap);System.out.println("流水记录ID;收入;支出;余额");StringBufferbuffer=newStringBuffer();Set<Integer>dataSet=dataMap.keySet();Iterator<Integer>it=dataSet.iterator();while(it.hasNext()){intid=it.next();Datadata=dataMap.get(id);buffer.append(id+";");buffer.append(Data.formatNumber(data.getIncome())+";");buffer.append(Data.formatNumber(data.getExpenditure())+";");buffer.append(Data.formatNumber(data.getBalance())+"n");}System.out.println(buffer.toString().replace("-1","?"));}}
问题:为什么就一直失败呢?本地测试比较少的数据没有问题呀------失败也没有失败原因的具体提示,好纠结呀!!
解决方案
解决方案二:
相关:http://bbs.csdn.net/topics/390874780?page=1