问题描述
求PHP翻译成C#<?php$prize_arr=array('0'=>array('id'=>1,'prize'=>'一','v'=>5),'1'=>array('id'=>2,'prize'=>'二','v'=>5),'2'=>array('id'=>3,'prize'=>'三','v'=>5),'3'=>array('id'=>4,'prize'=>'四','v'=>5),'4'=>array('id'=>5,'prize'=>'五','v'=>5),'5'=>array('id'=>6,'prize'=>'六','v'=>5),'6'=>array('id'=>7,'prize'=>'七','v'=>5),'7'=>array('id'=>8,'prize'=>'八','v'=>5),'8'=>array('id'=>9,'prize'=>'九','v'=>5),'9'=>array('id'=>10,'prize'=>'十','v'=>5),'10'=>array('id'=>11,'prize'=>'十一','v'=>25),'11'=>array('id'=>12,'prize'=>'十二','v'=>25),);foreach($prize_arras$k=>$v){$arr[$v['id']]=$v['v'];}$prize_id=getRand($arr);foreach($prize_arras$k=>$v){if($v['id']==$prize_id){$prize_site=$k;break;}}$res=$prize_arr[$prize_id-1];$data['prize_name']=$res['prize'];$data['prize_site']=$prize_site;$data['prize_id']=$prize_id;echojson_encode($data);functiongetRand($proArr){$data='';$proSum=array_sum($proArr);foreach($proArras$k=>$v){$randNum=mt_rand(1,$proSum);if($randNum<=$v){$data=$k;break;}else{$proSum-=$v;}}unset($proArr);return$data;}
解决方案
解决方案二:
Idon'tknowofanytoolthatcanconvertfromPHPtoC#.Ifthereissuchathing,Iwouldn'ttrustittodotheconversioncorrectly.Sothatleavesyouroptionsto:-LearnC#-Passittosomeonewhocanconvertit.
解决方案三:
你的这个代码是概率抽奖但是代码写的太烂,先给你优化一下$prize_arr=array('0'=>array('id'=>1,'prize'=>'一','Value'=>5),'1'=>array('id'=>2,'prize'=>'二','Value'=>5),'2'=>array('id'=>3,'prize'=>'三','Value'=>5),'3'=>array('id'=>4,'prize'=>'四','Value'=>5),'4'=>array('id'=>5,'prize'=>'五','Value'=>5),'5'=>array('id'=>6,'prize'=>'六','Value'=>5),'6'=>array('id'=>7,'prize'=>'七','Value'=>5),'7'=>array('id'=>8,'prize'=>'八','Value'=>5),'8'=>array('id'=>9,'prize'=>'九','Value'=>5),'9'=>array('id'=>10,'prize'=>'十','Value'=>5),'10'=>array('id'=>11,'prize'=>'十一','Value'=>25),'11'=>array('id'=>12,'prize'=>'十二','Value'=>25),);echojson_encode(ProbabilityLottery($prize_arr));functionProbabilityLottery($proArr){$proSum=0;foreach($proArras$x)$proSum+=$x['Value'];foreach($proArras$v){$randNum=mt_rand(1,$proSum);if($randNum<=$v['Value']){break;}else{$proSum-=$v['Value'];}}return$v;}
这样移植起来方便些
解决方案四:
staticvoidMain(string[]args){varprize_arr=newList<object>(){new{id=1,prize="一",Value=5},new{id=2,prize="二",Value=5},new{id=3,prize="三",Value=5},new{id=4,prize="四",Value=5},new{id=5,prize="五",Value=5},new{id=6,prize="六",Value=5},new{id=7,prize="七",Value=5},new{id=8,prize="八",Value=5},new{id=9,prize="九",Value=5},new{id=10,prize="十",Value=5},new{id=11,prize="十一",Value=25},new{id=12,prize="十二",Value=25},};for(inti=0;i<10;i++){vars=ProbabilityLottery(prize_arr);Console.WriteLine("{{"id":"{0}","prize":"{1}","v":"{2}"}}",s.id,s.prize,s.Value);}Console.ReadKey();}staticRandomrand=newRandom();staticdynamicProbabilityLottery(List<object>proArr){varproSum=0;foreach(dynamicxinproArr)proSum+=x.Value;foreach(dynamicvinproArr){varrandNum=rand.Next(1,proSum);if(randNum<=v.Value){returnv;}else{proSum-=v.Value;}}returnnew{};}
你可以看到:基本就是直译php也是C语言风格的