java题目随机代码,java随机抽题
求教大佬!java实现随机出题
public class test {
宝山网站建设公司创新互联建站,宝山网站设计制作,有大型网站制作公司丰富经验。已为宝山1000多家提供企业网站建设服务。企业网站搭建\成都外贸网站制作要多少钱,请找那个售后服务好的宝山做网站的公司定做!
public static void main(String[] args) {
// java实现随机出题
// 50以内的加减乘除法,用户解答后系统判断对错,用户输入-1时中止训练
Scanner scan = new Scanner(System.in);
char[] arr = {'+','-','*','÷'};
while (true) {
// 生成一个随机小数,范围从 2.25 到 2.75的两位小数
// double sum = Math.random()*(2.75-2.25)+2.25;
int a = (int) (Math.random()*(50-0)+0);
int b = (int) (Math.random()*(50-0)+0);
int f = (int) (Math.random()*(4-0)+0);
if (f== 1 a b) {
continue;
}
if (f== 3 a == 0) {
continue;
}
System.out.println("题目:"+a+arr[f]+b+"=\n请输入:");
double sum = 0;
if (f == 0) {
sum = a+b;
}
if (f == 1) {
sum = a-b;
}
if (f == 2) {
sum = a*b;
}
if (f == 3) {
sum = 1.0*a/b;
}
double s = scan.nextDouble();
if (s == -1) {
System.err.println("结束");
break;
}
if (s == sum) {
System.err.println("正确");
}else {
System.err.println("错误");
}
}
}
}
Java随机数代码怎么写
Math.random()是令系统随机选取大于等于 0.0 且小于 1.0 的伪随机 double 值,是Java语言常用代码。
例如:var a:Number=Math.random()*2+1,设置一个随机1到3的变量。
Java随机抽取人名完整代码是什么?
public class test {\x0d\x0a public static void main(String[] args) {\x0d\x0a //定义人名数组\x0d\x0a String [] name = {"张三","李四","王五","八神庵","不知火舞","大蛇","景天","唐雪见","李逍遥","赵灵儿"};\x0d\x0a//随机生成数组下标、\x0d\x0a int num = (int)(Math.random() * 1000);\x0d\x0a//对生成的随机数进行判断,如果小于数组下标,就跳出循环\x0d\x0awhile (numname.length-1) {\x0d\x0a if (num
回答于 2022-11-16
如何用Java实现随机出题?
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/html;charset=GBK");
PrintWriter printwriter = httpservletresponse.getWriter();
printwriter.println("htmlhead/headbodycenter");
printwriter.println("请以POST方式提交");
printwriter.println("/center/body/html");
printwriter.close();
}
public void doPost(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws ServletException, IOException
{
httpservletresponse.setContentType("text/html;charset=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 = 0; k1 i; k1++)
{
int k = ai.length; //8
for(int l1 = 0; l1 total_question_in_paper; l1++)
{
// 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;
}
}
(java)题库中有20道题,随机抽10道不同的题出来 怎么写代码 在线等啊
import java.util.*;
class Untitled {
public static void main(String[] args) throws Exception {
Integer[] questions = generateQuestions();
for(Integer question : questions) {
System.out.println(question);
}
}
private static Integer[] generateQuestions() {
ListInteger questionNumbers = new ArrayListInteger();
Random rand = new Random();
int questionNumber, i = 0;
while(i 10) {
questionNumber = rand.nextInt(21);
if(!questionNumbers.contains(questionNumber)) {
questionNumbers.add(questionNumber);
i ++;
}
}
return questionNumbers.toArray(new Integer[10]);
}
}
用java随机出100道题
1、首先对这几百道题逐一编号(假如有500道)。
2、用一个数组存放这些编号(从1-500)
3、用以下代码
ListInteger noList = new ArrayListInteger();
//把500个编号放入noList中
for(int i=1; i=500; i++)
{
noList.add(i);
}
int inx = 100;
//存放被随机选中的题目编号
ListInteger selectList = new ArrayListInteger();
while(inx0)
{
Random r = new Random();
int s = r.nextInt(noList.size());
selectList.add(noList.remove(s));
inx--;
}
本文名称:java题目随机代码,java随机抽题
新闻来源:http://pwwzsj.com/article/hcjhgp.html