// 获取min - max之间 n个随机数,然后排序
public static int[] orderedRandom(int min, int max, int n) {
if (n >(max - min + 1) || max <min) {
return null
}
int[] result = new int[n]
int count = 0
while (count <n) {
int num = (int) (Math.random() * (max - min)) + min
boolean flag = true
for (int j = 0j <nj++) {
if (num == result[j]) {
flag = false
break
}
}
if (flag) {
result[count] = num
count++
}
}
return Arrays.stream(result).sorted().toArray()
}
package ccut.lxn.BaiduKnowsimport java.io.BufferedReader
import java.io.InputStreamReader
import java.util.Random
public class RandomQuestion {
public static void main(String[] args) {
Random random = new Random()// 声明实例化一个random对象。
int a = random.nextInt(50)// 随机出现0-50间的数字。
int b = random.nextInt(50)
boolean flag = random.nextBoolean()
if (flag) {
System.out.print(a + " + " + b + " = ")
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in))// 从键盘上输入字符
try {// 抛出异常
String str = br.readLine()// 按字符串接收,接收键盘上输入的字符
if (str.matches("\\d+")) {// 正则表达式,为了当你输入除整数外的其他字符时,报错。
int result = Integer.parseInt(str)// 把输入的字符串转成int型
if (result == a + b) {
System.out.println("right")
} else {
System.out.println("error")
}
} else {
System.out.println("请输入数字!")
}
} catch (Exception e) {
System.out.println("出现错误:----->" + e.toString())
}
} else {
System.out.print(a + " - " + b + " = ")
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in))// 从键盘上输入字符
try {// 抛出异常
String str = br.readLine()// 按字符串接收,接收键盘上输入的字符
if (str.matches("\\d+")) {// 正则表达式,为了当你输入除整数外的其他字符时,报错。
int result = Integer.parseInt(str)// 把输入的字符串转成int型
if (result == a - b) {
System.out.println("right")
} else {
System.out.println("error")
}
} else {
System.out.println("请输入数字!")
}
} catch (Exception e) {
System.out.println("出现错误:----->" + e.toString())
}
}
}
}
ps:这是刚刚敲出来的,本来想优化下代码的,但是现在我要忙别的了(万分火急的),sorry,希望采纳,给我个鼓励。
import java.io.*import java.sql.*
import javax.servlet.*
import javax.servlet.http.*
import oracle.jdbc.driver.OracleDriver
public class GenPaperServlet extends HttpServlet
{
Connection conn
Statement stmt
ResultSet rs
int total_question_num
int total_question_in_paper
int total_paper_num
String curr_classid
public GenPaperServlet()
{
conn = null
stmt = null
rs = null
total_question_num = 0
total_question_in_paper = 0
total_paper_num = 0
curr_classid = ""
}
public void doGet(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws ServletException, IOException
{
httpservletresponse.setContentType("text/htmlcharset=GBK")
PrintWriter printwriter = httpservletresponse.getWriter()
printwriter.println("<html><head></head><body><center>")
printwriter.println("请以POST方式提交")
printwriter.println("</center></body></html>")
printwriter.close()
}
public void doPost(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws ServletException, IOException
{
httpservletresponse.setContentType("text/htmlcharset=GBK")
PrintWriter printwriter = httpservletresponse.getWriter()
String s = httpservletrequest.getParameter("classid") //"20"
String s1 = httpservletrequest.getParameter("paper_num") //"1"
if(s == null || s1 == null)
{
printwriter.println("<center>")
printwriter.println("请按照正常方式提交数据<br>")
printwriter.println("<a href=/test/admin/genpaper.jsp>单击这里设置生成试卷的参数</a>")
printwriter.println("</center>")
}
total_paper_num = Integer.parseInt(s1)
curr_classid = s
int i = 0
if(!open_db(curr_classid))
{
printwriter.println("打开数据库错误!")
return
}
if(!setParams(curr_classid))
{
System.out.println("设置系统参数错误!")
return
}
if(!verify_QuertionLib())
{
printwriter.println("试题库中试卷不足,请增加新的试题!")
printwriter.println("班级代号:" + curr_classid)
printwriter.println("该班级一套试卷中的试题数:" + total_question_in_paper)
printwriter.println("目前题库中该班级的试题总数:" + total_question_num)
return
}
i = genPaper(total_paper_num, curr_classid)
if(i == 0)
{
printwriter.println("生成试卷 *** 作失败!")
return
}
if(!updateOtherTable(i, curr_classid))
{
printwriter.println("更新相关表 *** 作失败!")
return
} else
{
printwriter.println("<center>")
printwriter.println("动态组卷成功!<br>")
printwriter.println("共生成了 " + i + " 套试卷<br>")
printwriter.println("<a href=/test/admin/genpaper.jsp>单击这里设置生成试卷的参数</a>")
printwriter.println("</center>")
return
}
}
public boolean open_db(String s)
{
try
{
new OracleDriver()
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ora9", "scott", "tiger")
stmt = conn.createStatement()
}
catch(Exception exception)
{
return false
}
return true
}
public boolean setParams(String s)
{
String s1 = ""
try
{
String s2 = "select count(questionid) as countquestionid from test_question_lib "
s2 = s2 + "where classid='" + s + "'"
rs = stmt.executeQuery(s2)
rs.next()
total_question_num = rs.getInt("countquestionid")
s2 = "select totalques from test_classinfo "
s2 = s2 + "where classid='" + s + "'"
rs = stmt.executeQuery(s2)
rs.next()
total_question_in_paper = rs.getInt("totalques")
}
catch(Exception exception)
{
return false
}
return true
}
public boolean verify_QuertionLib()
{
return total_question_num >= total_question_in_paper
}
public boolean updateOtherTable(int i, String s)
{
int j = 0
String s1 = "update test_classinfo set totalpaper=totalpaper+"
s1 = s1 + i + " where classid='" + s + "'"
try
{
j = stmt.executeUpdate(s1)
}
catch(Exception exception)
{
return false
}
return j == 1
}
public int genPaper(int i, String s)
{
boolean flag = false
boolean flag1 = false
boolean flag2 = false
boolean flag3 = false
String s1 = ""
try
{
int ai[] = new int[total_question_num]
int i1 = 0
boolean flag4 = false
String s2 = "select max(paper_id) as max_paper_id from test_paper_lib"
rs = stmt.executeQuery(s2)
rs.next()
int j = rs.getInt("max_paper_id") + 1
s2 = "select questionid from test_question_lib where classid='" + s + "'"
for(rs = stmt.executeQuery(s2)rs.next())
ai[i1++] = rs.getInt("questionid")
for(int k1 = 0k1 <ik1++)
{
int k = ai.length//8
for(int l1 = 0l1 <total_question_in_paperl1++)
{
// int ai[] ={1 ,3 , 5 ,9 , 56,30 96 ,25}
int j1 = (int)Math.floor(Math.random() * (double)k)// 4
String s3 = "insert into test_paper_lib values("
s3 = s3 + "test_seq_paper.nextval," + j + "," + ai[j1] + ")"
stmt.executeUpdate(s3)
int l = ai[k - 1]
ai[k - 1] = ai[j1]
ai[j1] = l
k--
}
j++
}
}
catch(Exception exception)
{
return 0
}
return i
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)