導航:首頁 > 編程語言 > 考試系統java代碼

考試系統java代碼

發布時間:2022-09-07 08:10:30

『壹』 學生考試管理系統,java源代碼

//主類EnglishTest——

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EnglishTest extends JFrame
{
TestArea testPanel=null;
Container con=null;
public EnglishTest()
{
super("模擬考試");
testPanel=new TestArea();
con=getContentPane();
con.add(testPanel,BorderLayout.CENTER);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
});
setVisible(true);
setBounds(60,40,660,460);
con.validate();
validate();
}
public static void main(String args[])
{
new EnglishTest();
}
}

//讀取試題 ReadTestquestion

import java.io.*;
import java.util.*;
public class ReadTestquestion
{ String filename="",
correctAnswer="",
testContent="" ,
selection="" ;
int score=0;
long time=0;
boolean 完成考試=false;
File f=null;
FileReader in=null;
BufferedReader 讀取=null;
public void setFilename(String name)
{ filename=name;

score=0;
selection="";
try {
if(in!=null&&讀取!=null)
{
in.close();
讀取.close();
}
f=new File(filename);
in=new FileReader(f);
讀取=new BufferedReader(in);
correctAnswer=(讀取.readLine()).trim();
String temp=(讀取.readLine()).trim() ;
StringTokenizer token=new StringTokenizer(temp,":");
int hour=Integer.parseInt(token.nextToken()) ;
int minute=Integer.parseInt(token.nextToken());
int second=Integer.parseInt(token.nextToken());
time=1000*(second+minute*60+hour*60*60);

}
catch(Exception e)
{
testContent="沒有選擇試題";
}
}
public String getFilename()
{
return filename;
}
public long getTime()
{
return time;
}
public void set完成考試(boolean b)
{
完成考試=b;
}
public boolean get完成考試()
{
return 完成考試;
}
public String getTestContent()
{ try {
String s=null;
StringBuffer temp=new StringBuffer();
if(讀取!=null)
{
while((s=讀取.readLine())!=null)
{
if(s.startsWith("**"))
break;
temp.append("\n"+s);
if(s.startsWith("endend"))
{
in.close();
讀取.close();
完成考試=true;
}
}
testContent=new String(temp);
}
else
{
testContent=new String("沒有選擇試題");
}
}
catch(Exception e)
{
testContent="試題內容為空,考試結束!!";
}
return testContent;
}
public void setSelection(String s)
{
selection=selection+s;
}
public int getScore()
{ score=0;
int length1=selection.length();
int length2=correctAnswer.length();
int min=Math.min(length1,length2);
for(int i=0;i<min;i++)
{ try{
if(selection.charAt(i)==correctAnswer.charAt(i))
score++;
}
catch( e)
{
i=0;
}
}
return score;
}20:10 03-8-31
public String getMessages()
{
int length1=selection.length();
int length2=correctAnswer.length();
int length=Math.min(length1,length2);
String message="正確答案:"+correctAnswer.substring(0,length)+"\n"+
"你的回答:"+selection+"\n";
return message;
}

}

//考試區域TestArea

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
class FileName implements FilenameFilter
{
String str=null;
FileName (String s)
{
str="."+s;
}
public boolean accept(File dir,String name)
{
return name.endsWith(str);
}
}
public class TestArea extends JPanel implements ActionListener,ItemListener,Runnable
{
Choice list=null;
JTextArea 試題顯示區=null,消息區=null;
JCheckBox box[];
JButton 提交該題答案,讀取下一題,查看分數;
ReadTestquestion 讀取試題=null;
JLabel welcomeLabel=null;
Thread countTime=null;
long time=0;
JTextField timeShow=null;
boolean 是否關閉計時器=false,
是否暫停計時=false;
JButton 暫停或繼續計時=null;
public TestArea()
{
list= new Choice();
String 當前目錄=System.getProperty("user.dir");
File dir=new File(當前目錄);
FileName fileTxt=new FileName("txt");
String fileName[]=dir.list(fileTxt);
for(int i=0;i<fileName.length;i++)
{
list.add(fileName[i]);
}

試題顯示區=new JTextArea(15,12);
試題顯示區.setLineWrap(true);
試題顯示區.setWrapStyleWord(true);
試題顯示區.setFont(new Font("TimesRoman",Font.PLAIN,14));
試題顯示區.setForeground(Color.blue);
消息區=new JTextArea(8,8);
消息區.setForeground(Color.blue);
消息區.setLineWrap(true);
消息區.setWrapStyleWord(true);

countTime=new Thread(this);
String s[]={"A","B","C","D"};
box=new JCheckBox[4];
for(int i=0;i<4;i++)
{
box[i]=new JCheckBox(s[i]);
}
暫停或繼續計時=new JButton("暫停計時");
暫停或繼續計時.addActionListener(this);
提交該題答案=new JButton("提交該題答案");
讀取下一題=new JButton("讀取第一題");
讀取下一題.setForeground(Color.blue);
提交該題答案.setForeground(Color.blue);
查看分數=new JButton("查看分數");
查看分數.setForeground(Color.blue);
提交該題答案.setEnabled(false);
提交該題答案.addActionListener(this);
讀取下一題.addActionListener(this);
查看分數.addActionListener(this);
list.addItemListener(this);
讀取試題=new ReadTestquestion();
JPanel pAddbox=new JPanel();
for(int i=0;i<4;i++)
{
pAddbox.add(box[i]);
}
Box boxH1=Box.createVerticalBox(),
boxH2=Box.createVerticalBox(),
baseBox=Box.createHorizontalBox();
boxH1.add(new JLabel("選擇試題文件"));
boxH1.add(list);
boxH1.add(new JScrollPane(消息區));
boxH1.add(查看分數);
timeShow=new JTextField(20);
timeShow.setHorizontalAlignment(SwingConstants.RIGHT);
timeShow.setEditable(false);
JPanel p1=new JPanel();
p1.add(new JLabel("剩餘時間:"));
p1.add(timeShow);
p1.add(暫停或繼續計時);
boxH1.add(p1);
boxH2.add(new JLabel("試題內容:"));
boxH2.add(new JScrollPane(試題顯示區));
JPanel p2=new JPanel();
p2.add(pAddbox);
p2.add(提交該題答案);
p2.add(讀取下一題);
boxH2.add(p2);
baseBox.add(boxH1);
baseBox.add(boxH2);
setLayout(new BorderLayout());
add(baseBox,BorderLayout.CENTER);
welcomeLabel=new JLabel("歡迎考試,提高英語水平",JLabel.CENTER);
welcomeLabel.setFont(new Font("隸書",Font.PLAIN,24));
welcomeLabel.setForeground(Color.blue);
add(welcomeLabel,BorderLayout.NORTH);

}
public void itemStateChanged(ItemEvent e)
{
timeShow.setText(null);
是否關閉計時器=false;
是否暫停計時=false;
暫停或繼續計時.setText("暫停計時");
String name=(String)list.getSelectedItem();
讀取試題.setFilename(name);
讀取試題.set完成考試(false);
time=讀取試題.getTime();
if(countTime.isAlive())
{
是否關閉計時器=true;
countTime.interrupt();
}
countTime=new Thread(this);

消息區.setText(null);
試題顯示區.setText(null);
讀取下一題.setText("讀取第一題");
提交該題答案.setEnabled(false);
讀取下一題.setEnabled(true);
welcomeLabel.setText("歡迎考試,你選擇的試題:"+讀取試題.getFilename());
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==讀取下一題)
{
讀取下一題.setText("讀取下一題");
提交該題答案.setEnabled(true);
String contentTest=讀取試題.getTestContent();
試題顯示區.setText(contentTest);
消息區.setText(null);
讀取下一題.setEnabled(false);
try {
countTime.start();
}
catch(Exception event)
{

}
}
if(e.getSource()==提交該題答案)
{
讀取下一題.setEnabled(true);
提交該題答案.setEnabled(false);
String answer="?";
for(int i=0;i<4;i++)
{
if(box[i].isSelected())
{
answer=box[i].getText();
box[i].setSelected(false);
break;
}
}
讀取試題.setSelection(answer);
}
if(e.getSource()==查看分數)
{
int score=讀取試題.getScore();
String messages=讀取試題.getMessages();
消息區.setText("分數:"+score+"\n"+messages);
}
if(e.getSource()==暫停或繼續計時)
{
if(是否暫停計時==false)
{
暫停或繼續計時.setText("繼續計時");
是否暫停計時=true;
}
else if(是否暫停計時==true)
{
暫停或繼續計時.setText("暫停計時");
是否暫停計時=false;
countTime.interrupt();
}
}
}
public synchronized void run()
{
while(true)
{
if(time<=0)
{
是否關閉計時器=true;
countTime.interrupt();
提交該題答案.setEnabled(false);
讀取下一題.setEnabled(false);
timeShow.setText("用時盡,考試結束");
}
else if(讀取試題.get完成考試())
{
是否關閉計時器=true;
timeShow.setText("考試效果:分數*剩餘時間(秒)="+1.0*讀取試題.getScore()*(time/1000));
countTime.interrupt();
提交該題答案.setEnabled(false);
讀取下一題.setEnabled(false);

}
else if(time>=1)
{
time=time-1000;
long leftTime=time/1000;
long leftHour=leftTime/3600;
long leftMinute=(leftTime-leftHour*3600)/60;
long leftSecond=leftTime%60;
timeShow.setText(""+leftHour+"小時"+leftMinute+"分"+leftSecond+"秒");
}
try
{
Thread.sleep(1000);
}
catch(InterruptedException ee)
{
if(是否關閉計時器==true)
return ;
}
while(是否暫停計時==true)
{
try
{
wait();
}
catch(InterruptedException ee)
{
if(是否暫停計時==false)
{
notifyAll();
}
}
}
}
}

}

『貳』 求在線考試系統源代碼,做好的更好,用java語言寫的,連接mysql資料庫的,在線等,急!!謝謝

1.Java連接MySQL資料庫
Java連接MySql需要下載JDBC驅動MySQL-connector-java-5.0.5.zip(舉例,現有新版本)。然後將其解壓縮到任一目錄。我是解壓到D盤,然後將其目錄下的MySQL-connector-java-5.0.5-bin.jar加到classpath里,具體如下:
「我的電腦」-> 「屬性」 -> 「高級」 -> 「環境變數」,在系統變數那裡編輯classpath,將D:\MySQL-connector-java-5.0.5\MySQL-connector-java-5.0.5-bin.jar加到最後,在加這個字元串前要加「;」,以與前一個classpath區分開。然後確定。

package hqs;
import java.sql.*;
public class DataBasePractice {

public static void main(String[] args) {
//聲明Connection對象
Connection con;
//驅動程序名
String driver = "com.mysql.jdbc.Driver";
//URL指向要訪問的資料庫名mydata
String url = "jdbc:mysql://localhost:3306/mydata";
//MySQL配置時的用戶名
String user = "root";
//MySQL配置時的密碼
String password = "root";
//遍歷查詢結果集
try {
//載入驅動程序
Class.forName(driver);
//1.getConnection()方法,連接MySQL資料庫!!
con = DriverManager.getConnection(url,user,password);
if(!con.isClosed())
System.out.println("Succeeded connecting to the Database!");
//2.創建statement類對象,用來執行SQL語句!!
Statement statement = con.createStatement();
//要執行的SQL語句
String sql = "select * from student";
//3.ResultSet類,用來存放獲取的結果集!!
ResultSet rs = statement.executeQuery(sql);
System.out.println("-----------------");
System.out.println("執行結果如下所示:");
System.out.println("-----------------");
System.out.println(" 學號" + "\t" + " 姓名");
System.out.println("-----------------");

String name = null;
String id = null;
while(rs.next()){
//獲取stuname這列數據
name = rs.getString("stuname");
//獲取stuid這列數據
id = rs.getString("stuid");
//首先使用ISO-8859-1字元集將name解碼為位元組序列並將結果存儲新的位元組數組中。
//然後使用GB2312字元集解碼指定的位元組數組。
name = new String(name.getBytes("ISO-8859-1"),"gb2312");
//輸出結果
System.out.println(id + "\t" + name);
}
rs.close();
con.close();
} catch(ClassNotFoundException e) {
//資料庫驅動類異常處理
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch(SQLException e) {
//資料庫連接失敗異常處理
e.printStackTrace();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
System.out.println("資料庫數據成功獲取!!");
}
}

}

2.添加、修改、刪除操作
在上面while代碼段後面添加以下代碼段:String name = null;
String id = null;
while(rs.next()){
//獲取stuname這列數據
name = rs.getString("stuname");
//獲取stuid這列數據
id = rs.getString("stuid");
//首先使用ISO-8859-1字元集將name解碼為位元組序列並將結果存儲新的位元組數組中。
//然後使用GB2312字元集解碼指定的位元組數組。
name = new String(name.getBytes("ISO-8859-1"),"gb2312");
//輸出結果
System.out.println(id + "\t" + name);
}

PreparedStatement psql;
ResultSet res;
//預處理添加數據,其中有兩個參數--「?」
psql = con.prepareStatement("insert into student values(?,?)");
psql.setInt(1, 8); //設置參數1,創建id為5的數據
psql.setString(2, "xiaogang"); //設置參數2,name 為小明
psql.executeUpdate(); //執行更新

//預處理更新(修改)數據
psql = con.prepareStatement("update student set stuname = ? where stuid = ?");
psql.setString(1,"xiaowang"); //設置參數1,將name改為王五
psql.setInt(2,10); //設置參數2,將id為2的數據做修改
psql.executeUpdate();

//預處理刪除數據
psql = con.prepareStatement("delete from student where stuid = ?");
psql.setInt(1, 5);
psql.executeUpdate();

//查詢修改數據後student表中的數據
psql = con.prepareStatement("select*from student");
res = psql.executeQuery(); //執行預處理sql語句
System.out.println("執行增加、修改、刪除後的數據");
while(res.next()){
name = res.getString("stuname");
id = res.getString("stuid");
name = new String(name.getBytes("ISO-8859-1"),"gb2312");
System.out.println(id + "\t" + name);
}
res.close();
psql.close();

該代碼段使用到了預處理語句:con.prepareStatement(String sql);
這樣生成資料庫底層的內部命令,並將該命令封裝在preparedStatement對象中,可以減輕資料庫負擔,提高訪問資料庫速度。 運行結果:

『叄』 誰有JavaWeb版本的在線考試系統,求完整源代碼。

基於java web的在線考試系統,我有的。資料庫:mysql
主要功能:注冊、登錄 考試 查詢 等功能

『肆』 請問怎麼樣才能讀取txt文件,我的代碼需要更改嗎,這是Java標准化考試系統的代碼,請求大佬的幫助!!

你這變數命名也太隨意了吧,都用上漢字了,並且你貼出的代碼與你問的問題沒有什麼關系,只是讀取了正確答案,和小時,分鍾的信息;試題的內容也是放到這個file文件中了么?讀取txt的方法很簡單,看你寫的代碼,應該你也是會寫讀取的啊,用一個while循環循環讀取每一行的內容就行了嘛,如果真的不會,網路上相關代碼真的數不勝數了,隨便搜一個改一下就行。

『伍』 java在線考試系統的考試界面要個時間限制,怎麼弄(jsp)

jsp頁面做一個倒計時的提示就可以控制時間了。
實現方法如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<div id="showTimes"></div>
<%
long current_time=System.currentTimeMillis();
long end_time=1337875200000l;
long time=end_time-current_time;
%>
<script>
var second = <%= time / 1000%>; // 剩餘秒數
// 寫一個方法,將秒數專為天數
var toDays = function(){
var s = second % 60; // 秒
var mi = (second - s) / 60 % 60; // 分鍾
var h = ((second - s) / 60 - mi ) / 60 % 24; // 小時
var d = (((second - s) / 60 - mi ) / 60 - h ) / 24 // 天
return "剩餘:" + d + "天" + h + "小時" + mi + "分鍾" + s + "秒";
}
//然後寫一個定時器
window.setInterval(function(){
second --;
document.getElementById("showTimes").innerHTML = toDays ();
}, 1000);
</script>

『陸』 求高手幫做一個Java的考試系統 最少要有登錄 注冊 考試三個界面 恩還要能和資料庫連接 只要程

package exam;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class DB {

static String url = "jdbc:mysql://localhost/test";
static String user = "root";
static String password = "root";

public static Connection get() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
}

public static void register(String name, String password) {
String sql = "insert into user(name,password)values('" + name + "','"
+ password + "')";
Statement ps;
try {
ps = get().createStatement();
ps.execute(sql);
} catch (Exception e) {
e.printStackTrace();
}
}

public static boolean getUser(String name, String password) {

String sql = "select * from user where name=? and password=?";
try {
PreparedStatement ps = get().prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, password);
ResultSet rs = ps.executeQuery();

while (rs.next()) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}

package exam;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Login extends JFrame {

private static final long serialVersionUID = 7661734873254043071L;

private JLabel lbName = new JLabel("用戶名:");
private JLabel lbPassword = new JLabel("密 碼:");
private JTextField tfName = new JTextField();
private JPasswordField tfPsw = new JPasswordField();
private JButton btnLogin = new JButton("登錄");
private JButton btnReg = new JButton("注冊");
private JButton btnCancel = new JButton("取消");

public Login() {

JPanel p1 = new JPanel();
JPanel p3 = new JPanel();

p1.setLayout(new GridLayout(2, 2));

p1.add(lbName);
p1.add(tfName);
p1.add(lbPassword);
p1.add(tfPsw);
p3.add(btnLogin);
p3.add(btnReg);
p3.add(btnCancel);

this.add(p1, BorderLayout.CENTER);
this.add(p3, BorderLayout.SOUTH);

initFrame();
initListener();
}

private void initFrame() {
this.setSize(250, 130);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}

private void initListener() {

getRootPane().setDefaultButton(btnLogin);

btnLogin.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (DB.getUser(tfName.getText(), tfPsw.getText())) {
new MainFrame();
Login.this.setVisible(false);
} else {
JOptionPane.showMessageDialog(null, "用戶名或密碼不正確");
}
}
});

btnCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});

btnReg.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Regiter();
}
});
}

public static void main(String[] args) {
new Login();
}

}

package exam;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class MainFrame extends JFrame {

private static final long serialVersionUID = 4834094501063442998L;

public MainFrame() {
this.add(new JLabel("歡迎"));
initFrame();
}

private void initFrame() {
this.setSize(400, 300);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}

package exam;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Regiter extends JFrame {

private static final long serialVersionUID = -4617064242063406043L;

private JLabel lbName = new JLabel("用戶名");
private JLabel lbPassword = new JLabel("密 碼");
private JTextField tfName = new JTextField();
private JPasswordField tfPsw = new JPasswordField();
private JLabel lbConfirmPsw = new JLabel("確認密碼");
private JPasswordField tfConfirmPsw = new JPasswordField();
private JButton btnReg = new JButton("注冊");
private JButton btnCancel = new JButton("取消");

public Regiter() {
JPanel p1 = new JPanel();
JPanel p4 = new JPanel();

p1.setLayout(new GridLayout(3, 2));

p1.add(lbName);
p1.add(tfName);

p1.add(lbPassword);
p1.add(tfPsw);

p1.add(lbConfirmPsw);
p1.add(tfConfirmPsw);

p4.add(btnReg);
p4.add(btnCancel);

this.add(p1, BorderLayout.CENTER);
this.add(p4, BorderLayout.SOUTH);
initFrame();
initListener();
}

private void initListener() {

getRootPane().setDefaultButton(btnReg);

btnReg.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String psw = tfPsw.getText();
String confirmPsw = tfConfirmPsw.getText();

if (tfName.getText().trim() == null) {
JOptionPane.showMessageDialog(null, "用戶名不能為空");
return;
}
if (psw.trim() == null) {
JOptionPane.showMessageDialog(null, "密碼不能為空");
return;
}
if (confirmPsw.trim() == null) {
JOptionPane.showMessageDialog(null, "確認密碼不能為空");
return;
}

if (psw.equals(confirmPsw)) {
DB.register(tfName.getText(), psw);
JOptionPane.showMessageDialog(null, "注冊成功");
new MainFrame();
Regiter.this.setVisible(false);
} else {
JOptionPane.showMessageDialog(null, "兩次密碼不一致");
}
}
});

btnCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}

private void initFrame() {
this.setSize(260, 160);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}

總共4個類,資料庫訪問已經寫好了
mysql 用戶名和密碼都是root 只要加一個mysql.jar就行了。
考試內容不知道什麼,沒寫了!

『柒』 跪求java實現B/S考試系統的思路(最好有源代碼)

做這個題庫系統需要按照mvc設計一下, 最近我也實現一個類似的, 下面說下我的設計

閱讀全文

與考試系統java代碼相關的資料

熱點內容
軍營訓練不聽教官的命令 瀏覽:258
v開頭的音樂播放器是什麼APP 瀏覽:117
單片機是怎麼做出來的 瀏覽:315
博圖怎麼作為opc伺服器 瀏覽:100
編譯做題軟體 瀏覽:293
橋梁檢測pdf 瀏覽:685
化解壓力的一種方法 瀏覽:680
路由器和DSN伺服器有什麼區別 瀏覽:547
android伸縮控制項 瀏覽:851
androidm3u8緩存 瀏覽:234
imphp開源知乎 瀏覽:707
清除網路通配符dos命令 瀏覽:837
鴻蒙系統怎麼快速換回安卓 瀏覽:712
pdf綠色虛擬列印機 瀏覽:213
androidtab框架 瀏覽:148
java轉php的時間戳 瀏覽:640
編譯libstdc依賴 瀏覽:659
清演算法人與原法人的區別 瀏覽:411
家庭裝修下載什麼app軟體 瀏覽:576
美食博主用什麼app拍視頻 瀏覽:817