mysql數據庫隨機生成某個范圍內的數據


一、完成內容

1. 根據組卷規則隨機生成試題

2. 獲取考生答案,計算分數

3. 判斷答案正誤,錯題存到錯題表

4. 判斷考試是否第一次答題,若不是,使用第一次生成的試題

 

二、功能截圖

 

 

 

 

 

 

 

 

 

 

 三、核心代碼

/**
     * @param context 頁面傳過來的題號及其對應的答案,形式是 題號:ABCD
     */
    @Override
    public Integer stuTest(String testId, String context, String stuname) {
        // TODO Auto-generated method stub
        //聲明一個變量分數
        int rightNum =0;
        String bids="";
        //處理答案,計算分數
        //1.處理文本
        String str[] = context.split(",");
        
        for (int i = 0; i < str.length; i++) {
            
            //獲取對應的試題id
            String bid = str[i].substring(0, str[i].indexOf(":"));
            //獲取對應答案
            
            String answer = str[i].substring(str[i].indexOf(":")+1);
            
            //根據bid查詢出題庫中正確的答案
            ItemBank itemBank = itemBankDao.selectByBid(new Integer(bid));
            //判斷答案是否一致
            answer = answer.replaceAll(" ", "");
            String itemBankAnswer = itemBank.getAnswer().replace(" ", "");
            
            if(answer.equalsIgnoreCase(itemBankAnswer)) {
                
                rightNum=rightNum+1;
                System.out.println("rightNum>>>"+rightNum);
            }else {
                
                //查詢判斷是否含有此bid
                MisCollection collection = misCollectionDao.selectByStuname(new Integer(stuname));
                if(collection!=null) {
                    String misbids = collection.getBids();
                    if(!misbids.contains(bid)) {
                        misbids+=","+bid;
                        misCollectionDao.update(misbids,new Integer(stuname));
                        
                    }
                    
                }else {
                    //第一次做題
                    
                        if(i==0) {
                            bids=bid;
                        }else {
                            bids+=","+bid;
                        }
                        
                        //將錯題存到表中
                        misCollectionDao.add(bids,new Integer(stuname));
                }
                
            }
            
        }
        
        
        
        
        Double score =  (double)((rightNum/str.length)*100);
        //將分數,答案更新到試卷表(testpaper)
        return testPaperDao.updateScoreAndAnswer(score,context,new Integer(testId),new Integer(stuname));
        
         
    }

四、數據庫隨機范圍查詢

<!-- 隨機生成試卷 -->
 <select id="makePaper" resultMap="itemBank">
 SELECT * FROM itembank
    WHERE bid >= (SELECT floor((select MIN(bid) from itembank where subid=#{0})+RAND() * (SELECT MAX(bid) FROM itembank where subid=#{0}))) 
    ORDER BY bid LIMIT #{1}
  
 </select>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM