java实训———百词斩

时间:2025-02-12 11:22:13

任务二:“百词斩”应用程序设计

实训内容:模仿“百词斩”手机App,设计并用Java语言实现一个“百词斩”图形界面程序(根据自己的能力,可以适当地增加或删除部分功能)。

最低要求:

(1)事先将一定数量的英语单词、四个选项(英语单词的汉语解释)及正确答案存放在一个文本文件中(一个单词的信息占一行,各项之间用一个空格分隔)。

(2)从文件中取出所有单词的信息,存入数组中。

(3)从单词数组中,随机找出一个单词,以单项选择题的形式显示出来,供用户答题。答对时显示√,答错时显示×并显示正确结果。每答完一题,都要统计并显示目前答对的单词数量。

(4)对于已经回答正确的单词,以后不会再出现。回答错误的单词,以后还会随机出现。

提示:

假如总共有n个单词,存入数组WORDS。

抽题时:

a. 随机产生一个[0,n)之间的随机整数i。

b. 取出WORDS[i]中的单词,作为选择题供用户选择。

答题时:

如果用户答对了,将WORDS[i]与WORDS[n-1]互换,n的值减1。

(5)所有单词都回答正确时n=0,程序结束。也可以关闭窗口强行退出。

import .*;
import .*;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
//设定选项以及单词信息
class Baicizhan implements ActionListener {
    //region 属性
    JFrame frame = new JFrame("百词斩");
    JPanel panel1;
    JPanel panel2;
    JPanel panel3;
    JLabel label1;  //设置标签一,显示答题数
    JLabel label2;  //设置标签二,用来显示单词
    JLabel label3 = new JLabel();//设置标签三,用来显示对错信息
    JButton jButton1;//上一题
    JButton jButton2;//下一题
    JRadioButton[] jrbs; //选项
    ButtonGroup group;//选项组
    Font f1 = new Font("宋体", , 30);
    int I;//随机的单词下标数
    int m = 0;//记录答对次数
    int count = 0;//记录答题次数
    String[] str;//复制随机数组
    ArrayList<String[]> arrayList;  //存放单词信息
    //窗体显示-构造方法
    public Baicizhan() throws FileNotFoundException {
        //随机的一个数
        I = (int)(()*4);
        //获取str
        getRandomStringArrays(I);
        //装载
        Ziti();//字体颜色,大小
        //显示界面
        (JFrame.EXIT_ON_CLOSE);//关闭即停止运行
        (400, 400);
        Point point = new Point(800, 300); // 设置坐标
        (point); //设置窗体坐标
        ();//确定最佳大小
        (true);
    }
    // 创建窗体的基本组件
    public void Ziti() {
        panel1 = new JPanel();
        panel2 = new JPanel();
        panel3 = new JPanel();//用来显示正确答案
        //设置3个面板的布局
        (new GridLayout(1, 1));
        (new GridLayout(6, 1));
        (new GridLayout(1, 2));
        //面板加入窗体中
        (panel1, );
        (panel2, );
        (panel3, );
        // 将标签一放入第一个面板中,同时设置字体
        label1 = new JLabel("答对单词数: 0/4");
        ();//设置字体为绿色
        (f1);//设置字体样式
        (label1, );
        //设置第二个面板
        label2 = new JLabel(str[0]);
        (f1);//设置字体格式
        (label2, );
        group = new ButtonGroup();
        jrbs = new JRadioButton[4];//存放选项的数组
        for (int i = 0; i < ; i++) {
            jrbs[i] = new JRadioButton(str[i+1]);
            jrbs[i].setFont(f1);//给每个选项添加字体
            (jrbs[i]);//将每个选项存放在 ButtonGroup中
            (jrbs[i]);//将选项加入面板中
            jrbs[i].addActionListener(this);//监听
        }
        ();//设置字体为绿色
        (f1);
        (label3);
        //设置面板三
        jButton1 = new JButton("上一题");
        (false);//启动按钮
        jButton2 = new JButton("下一题");
        //将按钮加入面板中
        (jButton1);
        (jButton2);
        //将自身注册为监听程序
        (this);
        (this);
    }
    //获取单词信息
    public ArrayList getWord() throws FileNotFoundException {
        String[] strings = new String[4];
        FileReader fr;
        BufferedReader f;
        String[] word1;
        String[] word2;
        String[] word3;
        String[] word4;
        ArrayList<String[]> arrayList = new ArrayList<>();//初始化
        f = new BufferedReader(new FileReader(""));
        try {
            for (int i = 0; i < ; i++) {
                strings[i] = ();//每次读取一行信息
            }
            //四个单词的信息分别放在四个数组中
            word1 = strings[0].split(" ");
            word2 = strings[1].split(" ");
            word3 = strings[2].split(" ");
            word4 = strings[3].split(" ");
            //将四个单词数组存放在集合中
            (word1);
            (word2);
            (word3);
            (word4);
        } catch (IOException e) {
            ();//打印异常报错及原因
        }
        return arrayList;
    }
    //获取随机单词
    public void getRandomStringArrays(int i) throws FileNotFoundException {
        arrayList = getWord();
        str = (i);
    }
    //第n次正确后得答对题数
    public String getLanel() {
        return "答对单词数:" + m + "/4";
    }

    //装载选项
    public void ZitiSelect() {
        for (int i = 0; i < ; i++) {
            jrbs[i].setText(str[i + 1]);//将4个元素显示出来
        }
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (().equals(str[5])) {
            ("√");
            ();//设置颜色为绿色
            m++;//答对单词一次,就会加一,下一次就会显示
            (getLanel());//重新设置答对单词数
        } else if (().equals("上一题")) {
            if(I ==0){
                I  = ()-1;//当前的小标为0时,上一题的下标为3
            }else{
                I--;
            }
            str = (I);
            (str[5]);//答案显示
            (str[0]);//重置单词
            ZitiSelect();//重置选项
        }
        else if (().equals("下一题")) {
            if (count < 4) {
                count++;
                if (I == () - 1) {
                    I = 0;//当当前下标为3时,下一题下标为0
                } else {
                    I++;
                }
                (true);//启动按钮
                str = (I);
                (str[0]);
                ZitiSelect();//重置选项
                ("");
            }
            if(count==4){
                new jieshuchuangti();
            }
            ();//清除上一次的选择
        }
        else {//答错了
            ("x"+str[5]);
            ();
        }
    }
    class jieshuchuangti extends JFrame implements  ActionListener{
        public jieshuchuangti() {
            JLabel jl = new JLabel("恭喜你,完成作业!");
            (f1);
            setLayout(new GridLayout(2,1));
            add(jl);
            pack();
            setLocation(650,650);
            setVisible(true);
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            (0);//点击按钮时结束
        }
    }
}
public class test {
    public static void main(String[] args) throws FileNotFoundException {
        Baicizhan baicizhan = new Baicizhan();
    }
}