乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      springboot項(xiàng)目,servlet實(shí)現(xiàn)一個(gè)轉(zhuǎn)盤抽獎(jiǎng)程序

       KyunraWang 2017-03-23

      繼上面第二個(gè)springboot項(xiàng)目后,今晚應(yīng)朋友需求幫忙寫個(gè)抽獎(jiǎng)程序,想著就用springboot+servlet去發(fā)布一個(gè)服務(wù)給前端直接調(diào)用或者后端直接調(diào)用。

      下面來介紹一下:

      1、不錯(cuò),依然需要一個(gè)Application的啟動(dòng)springboot的入口

      1. import org.springframework.boot.SpringApplication;  
      2. import org.springframework.boot.autoconfigure.SpringBootApplication;  
      3. import org.springframework.boot.web.servlet.ServletComponentScan;  
      4.   
      5. /** 
      6.  * Created by LK on 2016/5/7. 
      7.  */  
      8. @SpringBootApplication  
      9. @ServletComponentScan  
      10. public class SpringBootServletSampleApplication {  
      11.     public static void main(String[] args) {  
      12.         SpringApplication.run(SpringBootServletSampleApplication.class,args);  
      13.     }  
      14. }  

      特別的注意一下,是通過使用注解注冊(cè)Servlet    

      @ServletComponentScan


      2、下面就創(chuàng)建一個(gè)類來實(shí)現(xiàn)HttpServlet

      1. import javax.servlet.ServletException;  
      2. import javax.servlet.annotation.WebServlet;  
      3. import javax.servlet.http.HttpServlet;  
      4. import javax.servlet.http.HttpServletRequest;  
      5. import javax.servlet.http.HttpServletResponse;  
      6. import java.io.IOException;  
      7. import java.util.Random;  
      8.   
      9. /** 
      10.  * Created by LK on 2016/5/7. 
      11.  */  
      12. @WebServlet(urlPatterns = "/lottery/go",description = "請(qǐng)?jiān)谇岸薬jax調(diào)用或者直接httpclient直接調(diào)用,可以返回抽獎(jiǎng)結(jié)果,需要?jiǎng)e的功能可以繼續(xù)擴(kuò)展")  
      13. public class LotteryServlet extends HttpServlet{  
      14.     @Override  
      15.     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {  
      16.         this.doPost(req,resp);  
      17.     }  
      18.   
      19.     @Override  
      20.     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {  
      21.         Object[][] prizeArr = new  Object[][]{  
      22.                 //獎(jiǎng)品id,min,max,prize【獎(jiǎng)項(xiàng)】,v【中獎(jiǎng)率】  
      23.                 {1,1,14,"一等獎(jiǎng)",1},  
      24.                 {2,346,364,"一等獎(jiǎng)",1},  
      25.                 {3,16,44,"不要灰心",10},  
      26.                 {4,46,74,"神馬也沒有",10},  
      27.                 {5,76,104,"祝您好運(yùn)",10},  
      28.                 {6,106,134,"二等獎(jiǎng)",2},  
      29.                 {7,136,164,"再接再厲",10},  
      30.                 {8,166,194,"神馬也沒有",10},  
      31.                 {9,196,224,"運(yùn)氣先攢著",10},  
      32.                 {10,226,254,"三等獎(jiǎng)",5},  
      33.                 {11,256,284,"要加油哦",10},  
      34.                 {12,286,314,"神馬也沒有",10},  
      35.                 {13,316,344,"謝謝參與",10}  
      36.         };  
      37.         Object result[] = award(prizeArr);//抽獎(jiǎng)后返回角度和獎(jiǎng)品等級(jí)  
      38.         resp.setContentType("text/html;charset=UTF-8");  
      39.         resp.getWriter().write("{\"angle\":\""+result[0]+"\",\"msg\":\""+result[2]+"\"}");  
      40.         System.out.println("轉(zhuǎn)動(dòng)角度:"+result[0]+"\t獎(jiǎng)項(xiàng)ID:"+result[1]+"\t提示信息:"+result[2]);  
      41.     }  
      42.     //抽獎(jiǎng)并返回角度和獎(jiǎng)項(xiàng)  
      43.     public Object[] award(Object[][] prizeArr){  
      44.         //概率數(shù)組  
      45.         Integer obj[] = new Integer[prizeArr.length];  
      46.         for(int i=0;i<prizeArr.length;i++){  
      47.             obj[i] = (Integer) prizeArr[i][4];  
      48.         }  
      49.         Integer prizeId = getRand(obj); //根據(jù)概率獲取獎(jiǎng)項(xiàng)id  
      50.         //旋轉(zhuǎn)角度  
      51.         int angle = new Random().nextInt((Integer)prizeArr[prizeId][2]-(Integer)prizeArr[prizeId][1])+(Integer)prizeArr[prizeId][1];  
      52.         String msg = (String) prizeArr[prizeId][3];//提示信息  
      53.         return new Object[]{angle,prizeId,msg};  
      54.     }  
      55.   
      56.     //根據(jù)概率獲取獎(jiǎng)項(xiàng)  
      57.     public Integer getRand(Integer obj[]){  
      58.         Integer result = null;  
      59.         try {  
      60.             int  sum = 0;//概率數(shù)組的總概率精度  
      61.             for(int i=0;i<obj.length;i++){  
      62.                 sum+=obj[i];  
      63.             }  
      64.             for(int i=0;i<obj.length;i++){//概率數(shù)組循環(huán)  
      65.                 int randomNum = new Random().nextInt(sum);//隨機(jī)生成1到sum的整數(shù)  
      66.                 if(randomNum<obj[i]){//中獎(jiǎng)  
      67.                     result = i;  
      68.                     break;  
      69.                 }else{  
      70.                     sum -=obj[i];  
      71.                 }  
      72.             }  
      73.         } catch (Exception e) {  
      74.             e.printStackTrace();  
      75.         }  
      76.         return result;  
      77.     }  
      78. }  

      3、運(yùn)行Application的main之后,直接瀏覽器訪問 http://127.0.0.1:8080/lottery/Go  即可得到抽獎(jiǎng)結(jié)果

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多