📄
字号:
(1) 客户端程序:
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
//输入考号的文本框,通过键盘事件限制考生只能输入数字:
class 考号文本框 extends TextField implements KeyListener
{考号文本框(int n)
{super(n); //调用父类的构造方法确定文本框的长度。
this.addKeyListener(this); //增加键盘监视器。
}
public void keyPressed(KeyEvent e)//通过实现KeyListener接口限制客户的输入//范围。
{ int c=e.getKeyCode();
boolean b1=(c<'0'||c>'9'),
b2=(c!='.'),
b3=(c!=KeyEvent.VK_BACK_SPACE);
if(b1&&b2&&b3) //判断是否输入了非数字字符。
{String temp=
this.getText().substring(0,this.getText().indexOf(c));
this.setText(temp);
}
}
public void keyTyped(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
}
}
/*答题卡,一个答题卡中有A、B、C、D供选择使用,并通过ItemEvent事件
记录考生的选择。
*/
class 答题卡 extends Panel implements ItemListener
{ Checkbox box_a,box_b,box_c,box_d; // 代表答题卡中A、B、C、D选项的对象。
Label label;
int number; //答题卡中的试题题号。
CheckboxGroup group; //答题为单项选择题。
String answer="?"; //记录被选择的答案。
答题卡()
{ number=0;
group=new CheckboxGroup();
box_a=new Checkbox("A",false,group);
box_b=new Checkbox("B",false,group);
box_c=new Checkbox("C",false,group);
box_d=new Checkbox("D",false,group);
box_a.addItemListener(this);
box_b.addItemListener(this);
box_c.addItemListener(this);
box_d.addItemListener(this);
label=new Label("("+number+")");
add(label); add(box_a); add(box_b);
add(box_c); add(box_d);
}
//通过实现ItemListener接口获取考生的选择:
public void itemStateChanged(ItemEvent e)
{ Checkbox box=(Checkbox)e.getItemSelectable();//获取事件源。
answer=box.getLabel(); //获取选项的名字。
box.setFont(new Font("Courier",Font.BOLD,18));
box.setForeground(Color.red); //用特殊颜色和字体显示被选择的答案。
if(box_a!=box) //未被选中的保持原来的颜色和字体。
{box_a.setFont(
new Font("TimesRoman",Font.PLAIN,12));
box_a.setForeground(Color.black);
}
if(box_b!=box){box_b.setFont(
new Font("TimesRoman",Font.PLAIN,12));
box_b.setForeground(Color.black);
}
if(box_c!=box){box_c.setFont(
new Font("TimesRoman",Font.PLAIN,12));
box_c.setForeground(Color.black);
}
if(box_d!=box){box_d.setFont(
new Font("TimesRoman",Font.PLAIN,12));
box_d.setForeground(Color.black);
}
}
}
//主类,实现考试界面
public class ExaminationClient extends Applet
implements Runnable,ActionListener
{ TextField 姓名=new TextField(10); //输入姓名的文本框。
考号文本框 考号=new 考号文本框(18); //输入考号的文本框。
TextArea 试题区=new TextArea(); //试题区。
Socket socket=null; //和服务器建立连接的网络套接字。
DataInputStream in=null; //通过套接字连接读取服务器发送来的试题。
DataOutputStream out=null; //通过套接字提交给服务器考号、姓名、和答卷。
Button 开始考试=new Button("开始考试"),
提交答卷=new Button("提交答卷");
Thread thread=null; //通过一个单独的线程读取试题。
答题卡 题卡[]=new 答题卡[21]; //制作答题卡用的数组。
Choice choice=null;int N=0;
public void init()
{ setLayout(null);
choice=new Choice(); //选择一套考题。
N=Integer.parseInt(getParameter("总数"));
for(int i=1;i<=N;i++)//从web页上获取考题的名字。
{choice.add(getParameter(String.valueOf(i)));
}
Panel p=new Panel(); //制作答题卡用的面板。
p.setLayout(new GridLayout(22,1)); //根据试题的要求制作答题卡。
p.add(new Label("(一)选择填空题(共10小题):"));
for(int i=1;i<=10;i++)
{ 题卡[i]=new 答题卡();
题卡[i].label.setText("("+i+")");
p.add(题卡[i]);
}
p.add(new Label("(二)阅读理解:"));
for(int i=11;i<=15;i++)
{ 题卡[i]=new 答题卡();
int m=i-10;
题卡[i].label.setText("("+m+")");
p.add(题卡[i]);
}
ScrollPane scroll=new ScrollPane();
scroll.add(p); //将含有答题卡的面板放入一个滚动窗格中。
add(scroll);
add(试题区);
int height,width;
height=getSize().height; width=getSize().width;
//安排各个组件的位置:
scroll.setBounds(0,32,2*width/5,height-65);
试题区.setBounds(2*width/5+2,32,3*width/5-10,height-65);
试题区.setEditable(false);
Panel north=new Panel();
Label L1=new Label("输入姓名:",Label.CENTER);
Label L2=new Label("输入考号:",Label.CENTER);
north.add(L1);
north.add(姓名);
north.add(L2);
north.add(考号);
north.add(new Label("选择一套试题:"));
north.add(choice);
north.add(开始考试);
add(north);
north.setBounds(20,2,width-25,30);
Panel south=new Panel();
south.add(提交答卷);
add(south);
south.setBounds(2*width/5+2,height-35,3*width/5-4,30);
开始考试.addActionListener(this); //通过按扭事件通知服务器考试开始。
提交答卷.addActionListener(this); //通过按扭事件提交答卷。
}
public void start()
{
if (thread==null)
{thread=new Thread(this); //创建用于读取服务器信息的线程。
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
}
public void run()
{String s=null;
try
{socket=new Socket(this.getCodeBase().getHost(), 6331);
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
}
catch (IOException e){}
while(true) //读取服务器发来的试题。
{ try
{s=in.readUTF();
if(s!=null)
{试题区.append(s+"\n");
}
}
catch (IOException e)
{姓名.setText("已经和服务器断开");
break;
}
}
}
public void actionPerformed(ActionEvent e) //通过该按扭事件通知考试开始,
//以便服务器发送试题给考生。
{if (e.getSource()==开始考试)
{ //验证是否输入了姓名和考号:
String name1="必须输入姓名";
String kaohao1="必须输入考号";
name1=姓名.getText()+name1;
kaohao1=考号.getText()+kaohao1;
if (name1.startsWith("必须输入姓名")||
kaohao1.startsWith("必须输入考号"))
{姓名.setText("必须输入姓名");
考号.setText("必须输入考号");
}
else{
try{//将所选的试题题目发给服务器:
out.writeUTF("start:"+choice.getSelectedItem());
}
catch(IOException e1)
{
}
}
}
if (e.getSource()==提交答卷) //通过该按扭事件提交答卷。
{ StringBuffer s=new StringBuffer();
for(int i=1;i<=15;i++) //获取考生的答案。
{ s.append(题卡[i].answer);
}
try
{ //将答卷、姓名、考号按着一定格式发送给服务器:
out.writeUTF(new String(s)+
"#"+姓名.getText()+考号.getText()+"###");
}
catch(IOException e1){}
开始考试.setEnabled(false);
提交答卷.setEnabled(false);
}
}
}
(2)服务器端程序
import java.io.*;import java.net.*;
import java.util.*;
public class ExaminationServer
{ public static void main(String args[])
{ServerSocket server=null;
Socket you=null;
while(true)
{try
{ server=new ServerSocket(6331);
}
catch(IOException e1)
{System.out.println("正在监听");}
try {
you=server.accept(); //接收客户的套接字。
}
catch (IOException e)
{System.out.println("正在等待客户");
}
if(you!=null)
{
new Server_thread(you).start(); //为客户服务的线程开始工作。
}
else {
continue;
}
}
}
}
class Server_thread extends Thread
{ Socket socket=null;
File file=null;
DataOutputStream out=null;
DataInputStream in=null;int n=0;
String huida=null;
PrintStream file_out=null;
OutputStream out1=null;
String buffer=null;
byte b[]=new byte[100];
String 答案=null;
int 得分=0;
String 考试者=null;
Server_thread(Socket t)
{ socket=t;
try {in=new DataInputStream(socket.getInputStream());
out1=socket.getOutputStream();
out=new DataOutputStream(out1);
}
catch (IOException e)
{
}
}
public void run()
{ String s=null,s1=null;
while(true)
{
try{
s=in.readUTF(); //读取客户发来的信息。
if(s.startsWith("start:")) //如果收到的开始考试信息。
{//获取考生所选的试题题目:
String str=s.substring(s.indexOf(":")+1);
//建立到试题文件的输入流:
BufferedReader file_in=
new BufferedReader(new FileReader(str));
try {//首先读取试题文件的第一行,因为第一行是标准答案。
答案=file_in.readLine();
答案=答案.trim();
//然后将试题的其余部分发送给考生客户:
while( (s1=file_in.readLine())!=null)
{out.writeUTF(s1);
}
file_in.close();
}
catch (IOException e)
{
}
}
if(s.endsWith("#")) //获取考生提交的答卷,并评阅答卷:
{得分=0;
if(答案!=null)
{int n=s.indexOf("#"); //提交的答卷的结束位置
int m=s.indexOf("###");//姓名和考号的结束位置。
for(int i=0;i<10;i++)
if(s.charAt(i)==答案.charAt(i))
{ 得分=得分+2; }
for(int i=10;i<=n-1;i++)
if(s.charAt(i)==答案.charAt(i))
{得分=得分+4; }
考试者=s.substring(n+1,m); //获取考生的姓名和考号
//在当前目录下创建一个子目录:Student,用来存放各个考生的成绩:
File dir=new File("Student");
dir.mkdir();
//根据考生的姓名、考号创建一个文件对象
file=new File(dir,考试者+".txt");
file_out=new PrintStream(
new FileOutputStream(file));
//将考生的姓名、考号和成绩写入文件:
file_out.println("考试者:"+考试者);
file_out.println("你的分数:"+得分);
file_out.println("你的答题:"+s.substring(0,n));
file_out.println("标准答案:"+答案);
}
}
sleep(30);
}
catch(InterruptedException e)
{
}
catch (IOException e)
{
System.out.println("客户离开");
try{out.close();in.close();
}
catch (IOException e1){}
break;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -