当前位置: 代码迷 >> Java相关 >> 新人,大侠们帮小弟我看看
  详细解决方案

新人,大侠们帮小弟我看看

热度:8927   发布时间:2013-02-25 21:48:29.0
新人求助,大侠们帮我看看
谁能帮我写一个十分简单的程序,大约是这样:"做十道选择题,最后算出总分。"就这样,十分简单,我才开始学所以做不来。谁愿意帮帮我请联系我好吗:QQ654865909

------解决方案--------------------------------------------------------
package io;


import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class EnglishExam extends JFrame implements ActionListener {
String strReadLine;//每次从题库文件提出的一行题目加答案
File fileExam;//题库文件
FileReader fin;//文件字符输入流
BufferedReader bin;//缓冲式字符输入流
JButton startBtn,nextBtn;
JRadioButton[] radiobtn;
JTextField questionTfd,scoreTfd;
int score;
ArrayList<String>array=new ArrayList<String>();//存储下一个题目的问题,选项和答案部分
EnglishExam(){
super("英语单词学习");
setLayout(new FlowLayout());
scoreTfd=new JTextField(10);
scoreTfd.setEditable(false);
questionTfd=new JTextField(50);
questionTfd.setEditable(false);
startBtn=new JButton("重新联系");
startBtn.addActionListener(this);
nextBtn=new JButton("下一题");
nextBtn.addActionListener(this);
radiobtn=new JRadioButton[4];
ButtonGroup b=new ButtonGroup();//单选按钮的ButtonGrop
for(int i=0;i<radiobtn.length;i++){
radiobtn[i]=new JRadioButton("");
b.add(radiobtn[i]);
radiobtn[i].addActionListener(this);
}
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JPanel p4=new JPanel();
p1.add(new JLabel("题目"));
p1.add(questionTfd);
p2.add(new JLabel("选择答案"));
for(JRadioButton radion:radiobtn)
p2.add(radion);
p3.add(new JLabel("您的得分是:"));
p3.add(scoreTfd);
p4.add(startBtn);
p4.add(nextBtn);
add(p1);
add(p2);
add(p3);
add(p4);
setBounds(20,100,660,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();
try{
fileExam=new File(this.getClass().getResource("").getPath(),"English.txt");
fin=new FileReader(fileExam);//实例化文件字符流对象fin
bin=new BufferedReader(fin);//实例化缓冲式字符输入流bin
}catch(IOException e){
e.printStackTrace();
}
readQuestionFromeFile();
}
public void readQuestionFromeFile(){//从题库中提取练习题的方法,并将问题和选项显示在JFrame框架中
int i=0;
array.clear();//清空array中的内容
try{
strReadLine=bin.readLine();//读取题库中的第一行
if(!(strReadLine.startsWith("endend"))){
StringTokenizer token=new StringTokenizer(strReadLine,"#");
while(token.hasMoreTokens()){
array.add(token.nextToken());
//
}
questionTfd.setText(array.get(0));
//array中存储的strReadLine分隔符的第一个子串代表“题目”,将它显示在问题文本框中
for(int j=1;j<=4;j++){
radiobtn[j-1].setSelected(false);
radiobtn[j-1].setText(array.get(j));
//array中存储的第二个到第五个字符串,代表的是选择题的四个选项,将他们分别显示在四个单选按钮部分
}
}else if(strReadLine.startsWith("endend")){
//如果读取出来的一行字符串是endend,表示所有的联系已经做完,读取结束
questionTfd.setText("考试结束");
for(int j=0;j<4;j++){
radiobtn[j].setText("end");//考试结束
radiobtn[j].setSelected(false);
bin.close();
fin.close();//关闭流
}
}
}catch(Exception e){
questionTfd.setText("无试题文件");
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==startBtn){
score=0;
scoreTfd.setText("得分:"+score);
try{
fin=new FileReader(fileExam);//实例化fin
bin=new BufferedReader(fin);//实例化缓冲式字符输入流
}catch(IOException e1){
e1.printStackTrace();
}
readQuestionFromeFile();//从题库文件中读取第一个练习题,并将显示在JFrame中
for(int j=0;j<4;j++){
radiobtn[j].setEnabled(true);//将四个单选按钮设置为可用状态
radiobtn[j].setSelected(false);
}
}else if(e.getSource() instanceof JRadioButton){
//如果触发动作事件
  相关解决方案