java多线程实例1 限时回答问题

这是一个Java实现的GUI应用程序,用于展示一个限时答题系统。程序从test.txt文件中读取问题,用户需要在8秒内选择答案。正确回答会增加分数,并自动进入下一题。如果超时或者答错,将自动跳转到下一题。程序包含计时器、文本区域显示问题、复选框供用户选择答案,以及显示分数和剩余时间的标签。用户还可以选择重新开始答题。
摘要由CSDN通过智能技术生成

1.每个问题提供ABCD四个选择(单选)
2.两个问题之间是用减号(-)尾加前一问题的答案分隔(如:----D----)

public class Example{

	public static void main(String[] args) {
		StandardExamInTime win = new StandardExamInTime();
		win.setTitle("限时回答问题");
		win.setTestFile(new java.io.File("test.txt"));
		win.setMAX(8);
	}

}
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class StandardExamInTime extends JFrame implements ActionListener,ItemListener {
	File testFile;
	int MAX = 8;
	int maxTime = MAX,score=0;
	javax.swing.Timer time;//计时器
	JTextArea showQuesion;//显示试题
	JCheckBox choiceA,choiceB,choiceC,choiceD;
	JLabel showScore,showTime;
	String correctAnswer;//正确答案
	JButton reStart;
	FileReader inOne;
	BufferedReader inTwo;
	StandardExamInTime(){
		time = new javax.swing.Timer(1000,this);
		showQuesion = new JTextArea(2,16);
		setLayout(new FlowLayout());
		showScore = new JLabel("分数"+score);
		showTime = new JLabel(" ");
		add(showTime);
		add(new JLabel("问题:"));
		add(showQuesion);
		choiceA = new JCheckBox("A");
		choiceB = new JCheckBox("B");
		choiceC = new JCheckBox("C");
		choiceD = new JCheckBox("D");
		choiceA.addItemListener(this);
		choiceB.addItemListener(this);
		choiceC.addItemListener(this);
		choiceD.addItemListener(this);
		add(choiceA);
		add(choiceB);
		add(choiceC);
		add(choiceD);
		add(showScore);
		reStart = new JButton("再做一遍");
		reStart.setEnabled(false);
		add(reStart);
		reStart.addActionListener(this);
		setBounds(100,100,200,200);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}
	public void setMAX(int n){
		MAX = n;
	}
	public void setTestFile(File f){
		testFile = f;
		score = 0;
		try{
			inOne = new FileReader(testFile);
			inTwo = new BufferedReader(inOne);
			readOneQuesion();
			reStart.setEnabled(false);
		}
		catch(IOException exp){
			showQuesion.setText("没有选题");
		}
	}
	public void readOneQuesion(){
		showQuesion.setText(null);
		try{
			String s = null;
			while((s = inTwo.readLine())!=null){
				if(!s.startsWith("-"))
					showQuesion.append("\n"+s);
				else{
					s = s.replaceAll("-", "");
					correctAnswer = s;
					break;
				}
			}
			time.start();//启动计时
			if(s==null){
				inTwo.close();
				reStart.setEnabled(true);
				showQuesion.setText("题目完毕");
				time.stop();
			}
		}
		catch(IOException exp){}
	}
	public void itemStateChanged(ItemEvent e){
		JCheckBox box = (JCheckBox)e.getSource();
		String str = box.getText();
		boolean booOne = box.isSelected();
		boolean booTwo = str.compareToIgnoreCase(correctAnswer)==0;
		if(booOne&&booTwo){
			score++;
			showScore.setText("分数:"+score);
			time.stop();//停止计时
			maxTime = MAX;
			readOneQuesion();//读入下一题目
		}
		box.setSelected(false);
	}
	public void actionPerformed(ActionEvent e){
		if(e.getSource()==time){
			showTime.setText("剩:"+maxTime+"秒");
			maxTime--;
			if(maxTime <= 0){
				maxTime = MAX;
				readOneQuesion();//读入下一题目
			}
		}
		else if(e.getSource()==reStart){
			setTestFile(testFile);
		}
	}

}

注:
1.本题使用了GUI界面,增加了计时器线程。
2.必须在8秒内回答,若超过,则进入下一题
3.问题保存在test.txt文件中,这里没有提供,可自行添加。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值