开发者社区> 问答> 正文

PHP代码怎么转Java代码?

小旋风柴进 2016-03-09 15:16:17 1406

比如:

<?php
$prize_arr = array(
'0' => array('id'=>1,'prize'=>'平板电脑','v'=>3),
'1' => array('id'=>2,'prize'=>'数码相机','v'=>5),
'2' => array('id'=>3,'prize'=>'音箱设备','v'=>10),
'3' => array('id'=>4,'prize'=>'4G优盘','v'=>12),
'4' => array('id'=>5,'prize'=>'Q币10元','v'=>20),
'5' => array('id'=>6,'prize'=>'下次没准就能中哦','v'=>50),
);
foreach ($prize_arr as $key => $val) {
$arr[$val['id']] = $val['v'];
}
$rid = getRand($arr); /*根据概率获取奖项id*/
$res['msg'] = ($rid==6)?0:1; 
$res['prize'] = $prize_arr[$rid-1]['prize']; /*中奖项*/
echo json_encode($res);exit;
/*计算概率*/
function getRand($proArr) {
$result = '';
/*概率数组的总概率精度*/
$proSum = array_sum($proArr);

/*概率数组循环*/
foreach ($proArr as $key => $proCur) {
    /*相当于每次只能够抽取其中的一个奖项,没抽中就开始抽下一个奖项,直到最后*/
    $randNum = mt_rand(1, $proSum);
    if ($randNum <= $proCur) {
        /*表示抽中奖了,该奖品应该要减一*/
        $result = $key;
        break;
    } else {
        /*没有抽中就不能再抽了,直到敲到下一个金蛋*/
        $proSum -= $proCur;
    }
}
unset ($proArr);
return $result;
}
?>
Java PHP
分享到
取消 提交回答
全部回答(1)
  • 小旋风柴进
    2019-07-17 18:56:14

    代码如下:

    public class Guess {
    private int id;
    private String prize;
    private int v;
    public Guess(int id, String prize, int v) {
    super();
    this.id = id;
    this.prize = prize;
    this.v = v;
    }
    public i1nt getId() {
    return id;
    }
    public void setId(int id) {
    this.id = id;
    }
    public String getPrize() {
    return prize;
    }
    public void setPrize(String prize) {
    this.prize = prize;
    }
    public int getV() {
    return v;
    }
    
    public void setV(int v) {
        this.v = v;
    }
    }
    
    public class WinPrize {
    private int mes;
    private String prize;
    public int getMes() {
    return mes;
    }
    public WinPrize(int mes, String prize) {
    super();
    this.mes = mes;
    this.prize = prize;
    }
    public WinPrize() {
    super();
    // TODO Auto-generated constructor stub
    }
    @Override
    public String toString() {
    return "mes:" + mes + ", prize:" + prize;
    }
    public void setMes(int mes) {
    this.mes = mes;
    }
    public String getPrize() {
    return prize;
    }
    public void setPrize(String prize) {
    this.prize = prize;
    }
    
    }
    
    public class TestPHP {
    public void guess_prize(){
    Map prize_attr=new HashMap();
    Integer[] arr=null;
    prize_attr.put(0, new Guess(1,"平板电脑",3));
    prize_attr.put(1, new Guess(2,"数码相机",5));
    prize_attr.put(2, new Guess(3,"音箱设备",10));
    prize_attr.put(3, new Guess(4,"4G优盘",12));
    prize_attr.put(4, new Guess(5,"Q币10元",20));
    prize_attr.put(5, new Guess(6,"下次没准就能中哦",50));
    arr=new Integer[prize_attr.size()+1];
    for(Guess g:prize_attr.values()){
    //System.out.println(prize_attr.size());
    arr[g.getId()]=g.getV();
    }
    WinPrize res=new WinPrize();
    int rid=getRand(arr);
    if(prize_attr.get(rid)==null){
    res.setMes(0);
    res.setPrize("下次没准就能中哦");
    }else{
    res.setMes(rid==6?0:1);
    res.setPrize(prize_attr.get(rid).getPrize());
    }
    System.out.println(res);
    }
    
    public int getRand(Integer[] arr){
        int sum=0;
        int result=0;
        //System.out.println(arr);
        for(int i=1;i<arr.length;i++){
            sum+=arr[i];
        }
        for(int i=1;i<arr.length;i++){
            Random r=new Random();
            int random=r.nextInt(sum)+1;//arr长度为6,生成1-6的随机数
            if(random<=arr[i]){
                result=i;
                break;
            }else{
                sum-=arr[i];
            }
        }
    
        return result;
    }
    public static void main(String[] args) {
        new TestPHP().guess_prize();        
    }
    }
    0 0

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题