public class test {
寶山網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,寶山網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為寶山1000多家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的寶山做網(wǎng)站的公司定做!
public static void main(String[] args) {
// java實(shí)現(xiàn)隨機(jī)出題
// 50以內(nèi)的加減乘除法,用戶解答后系統(tǒng)判斷對(duì)錯(cuò),用戶輸入-1時(shí)中止訓(xùn)練
Scanner scan = new Scanner(System.in);
char[] arr = {'+','-','*','÷'};
while (true) {
// 生成一個(gè)隨機(jī)小數(shù),范圍從 2.25 到 2.75的兩位小數(shù)
// 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請(qǐng)輸入:");
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("結(jié)束");
break;
}
if (s == sum) {
System.err.println("正確");
}else {
System.err.println("錯(cuò)誤");
}
}
}
}
Math.random()是令系統(tǒng)隨機(jī)選取大于等于 0.0 且小于 1.0 的偽隨機(jī) double 值,是Java語(yǔ)言常用代碼。
例如:var a:Number=Math.random()*2+1,設(shè)置一個(gè)隨機(jī)1到3的變量。
public class test {\x0d\x0a public static void main(String[] args) {\x0d\x0a //定義人名數(shù)組\x0d\x0a String [] name = {"張三","李四","王五","八神庵","不知火舞","大蛇","景天","唐雪見","李逍遙","趙靈兒"};\x0d\x0a//隨機(jī)生成數(shù)組下標(biāo)、\x0d\x0a int num = (int)(Math.random() * 1000);\x0d\x0a//對(duì)生成的隨機(jī)數(shù)進(jìn)行判斷,如果小于數(shù)組下標(biāo),就跳出循環(huán)\x0d\x0awhile (numname.length-1) {\x0d\x0a if (num
回答于?2022-11-16
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("請(qǐng)以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("請(qǐng)按照正常方式提交數(shù)據(jù)br");
printwriter.println("a href=/test/admin/genpaper.jsp單擊這里設(shè)置生成試卷的參數(shù)/a");
printwriter.println("/center");
}
total_paper_num = Integer.parseInt(s1);
curr_classid = s;
int i = 0;
if(!open_db(curr_classid))
{
printwriter.println("打開數(shù)據(jù)庫(kù)錯(cuò)誤!");
return;
}
if(!setParams(curr_classid))
{
System.out.println("設(shè)置系統(tǒng)參數(shù)錯(cuò)誤!");
return;
}
if(!verify_QuertionLib())
{
printwriter.println("試題庫(kù)中試卷不足,請(qǐng)?jiān)黾有碌脑囶}!");
printwriter.println("班級(jí)代號(hào):" + curr_classid);
printwriter.println("該班級(jí)一套試卷中的試題數(shù):" + total_question_in_paper);
printwriter.println("目前題庫(kù)中該班級(jí)的試題總數(shù):" + total_question_num);
return;
}
i = genPaper(total_paper_num, curr_classid);
if(i == 0)
{
printwriter.println("生成試卷操作失??!");
return;
}
if(!updateOtherTable(i, curr_classid))
{
printwriter.println("更新相關(guān)表操作失??!");
return;
} else
{
printwriter.println("center");
printwriter.println("動(dòng)態(tài)組卷成功!br");
printwriter.println("共生成了 " + i + " 套試卷br");
printwriter.println("a href=/test/admin/genpaper.jsp單擊這里設(shè)置生成試卷的參數(shù)/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;
}
}
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]);
}
}
1、首先對(duì)這幾百道題逐一編號(hào)(假如有500道)。
2、用一個(gè)數(shù)組存放這些編號(hào)(從1-500)
3、用以下代碼
ListInteger noList = new ArrayListInteger();
//把500個(gè)編號(hào)放入noList中
for(int i=1; i=500; i++)
{
noList.add(i);
}
int inx = 100;
//存放被隨機(jī)選中的題目編號(hào)
ListInteger selectList = new ArrayListInteger();
while(inx0)
{
Random r = new Random();
int s = r.nextInt(noList.size());
selectList.add(noList.remove(s));
inx--;
}
分享名稱:java題目隨機(jī)代碼,java隨機(jī)抽題
文章鏈接:http://muchs.cn/article18/hcjhgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站改版、域名注冊(cè)、網(wǎng)站收錄、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)