⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testbyradom.java

📁 利用JAVA开发的简单考试系统
💻 JAVA
字号:
package model.condition.test;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Random;
import model.persistency.PersistentItem;

import model.connection.Connectable;
import model.connection.ConnectionAccess;
import model.persistency.ItemMapper;
/**
 * 继承Testable接口,用于产生随机试卷
 * @author Cynthia
 *
 */
public class TestByRadom implements Testable {
	private ArrayList test;
/**
 * 构造函数
 *
 */
	public TestByRadom(){
		test = new ArrayList();
	}
/**
  * 用于返回试卷
  * @return   ArrayList类型 返回试卷
  */
	public ArrayList getList(){
		return test;
	}

/**
 * 接收随机产生试卷的请求,一张试卷是15道题目
 */
	public void accept(){

		ItemMapper itemMap;
		int oid = 0;
		int c =0;
                int[] record = new int[15];
                int j=0;
                ArrayList re = new ArrayList();
		try{
                    Connectable con = new ConnectionAccess( "sun.jdbc.odbc.JdbcOdbcDriver");
                  //  itemMap = ItemMapper.getInstance(con);
                  itemMap = new ItemMapper(con);
               //每次产生15个题目放入试卷
                c = itemMap.getItemCount();
                while(re.size() != c){
                    do {
                        //随机产生一个0~99的id
                        Random random = new Random();
                        oid = random.nextInt(100);
                        System.out.println(oid);
                    } while (!itemMap.findItem(oid)); //如果产生的id不存在则再随机产生一个
                    if (itemMap.findItem(oid)) {
                       if(re.isEmpty()== true) {
                          //  System.out.println("1add oid");
                            re.add(oid);
                             record[j++] = oid;
                          //  System.out.println("re" + oid);
                        }
                        else { if (re.contains(oid) == false) {
                          //      System.out.println("add oid");
                                re.add(oid);
                                record[j++] = oid;
                          //      System.out.println("re" + oid);
                                          }
                            }
                    }
                }
                if(c>15)
                  for(int i =0;i<15;i++){
                      PersistentItem p = itemMap.getItemById(record[i]);
                      System.out.println(p.getAnswer());
                      test.add(p);}
               else for(int i = 0;i<c; i++){
                      PersistentItem p = itemMap.getItemById(record[i]);
                      System.out.println(p.getAnswer());
                      test.add(p); }
               } catch(SQLException e){
                System.out.println("sql exception in accept");}
                 catch(ClassNotFoundException l){
			System.out.println("class not found");}
         }
}//~~

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -