导航:首页 > 编程语言 > 考试系统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代码相关的资料

热点内容
pdf绿色虚拟打印机 浏览:211
androidtab框架 浏览:144
java转php的时间戳 浏览:635
编译libstdc依赖 浏览:655
清算法人与原法人的区别 浏览:407
家庭装修下载什么app软件 浏览:572
美食博主用什么app拍视频 浏览:813
ipone手机如何加密微信 浏览:354
自来水加密阀阀帽 浏览:431
华为交换机dhcp配置命令 浏览:315
androidbitmap缩小 浏览:272
单片机串口控制灯 浏览:84
大讯云服务器安装视频 浏览:785
华为算法领先世界 浏览:654
linux路由重启 浏览:566
php的模板编程 浏览:322
编译器原理与实现书 浏览:711
dos选择命令 浏览:19
apm固件编译到单片机 浏览:123
联通深蓝卡都包含什么app 浏览:266