java实训———百词斩

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

实训内容:模仿“百词斩”手机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 javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
//设定选项以及单词信息
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("宋体", Font.BOLD, 30);
    int I;//随机的单词下标数
    int m = 0;//记录答对次数
    int count = 0;//记录答题次数
    String[] str;//复制随机数组
    ArrayList<String[]> arrayList;  //存放单词信息
    //窗体显示-构造方法
    public Baicizhan() throws FileNotFoundException {
        //随机的一个数
        I = (int)(Math.random()*4);
        //获取str
        getRandomStringArrays(I);
        //装载
        Ziti();//字体颜色,大小
        //显示界面
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭即停止运行
        frame.setSize(400, 400);
        Point point = new Point(800, 300); // 设置坐标
        frame.setLocation(point); //设置窗体坐标
        frame.pack();//确定最佳大小
        frame.setVisible(true);
    }
    // 创建窗体的基本组件
    public void Ziti() {
        panel1 = new JPanel();
        panel2 = new JPanel();
        panel3 = new JPanel();//用来显示正确答案
        //设置3个面板的布局
        panel1.setLayout(new GridLayout(1, 1));
        panel2.setLayout(new GridLayout(6, 1));
        panel3.setLayout(new GridLayout(1, 2));
        //面板加入窗体中
        frame.add(panel1, BorderLayout.NORTH);
        frame.add(panel2, BorderLayout.CENTER);
        frame.add(panel3, BorderLayout.SOUTH);
        // 将标签一放入第一个面板中,同时设置字体
        label1 = new JLabel("答对单词数: 0/4");
        label1.setForeground(Color.GREEN);//设置字体为绿色
        label1.setFont(f1);//设置字体样式
        panel1.add(label1, BorderLayout.EAST);
        //设置第二个面板
        label2 = new JLabel(str[0]);
        label2.setFont(f1);//设置字体格式
        panel2.add(label2, BorderLayout.NORTH);
        group = new ButtonGroup();
        jrbs = new JRadioButton[4];//存放选项的数组
        for (int i = 0; i < jrbs.length; i++) {
            jrbs[i] = new JRadioButton(str[i+1]);
            jrbs[i].setFont(f1);//给每个选项添加字体
            group.add(jrbs[i]);//将每个选项存放在 ButtonGroup中
            panel2.add(jrbs[i]);//将选项加入面板中
            jrbs[i].addActionListener(this);//监听
        }
        label3.setForeground(Color.GREEN);//设置字体为绿色
        label3.setFont(f1);
        panel2.add(label3);
        //设置面板三
        jButton1 = new JButton("上一题");
        jButton1.setEnabled(false);//启动按钮
        jButton2 = new JButton("下一题");
        //将按钮加入面板中
        panel3.add(jButton1);
        panel3.add(jButton2);
        //将自身注册为监听程序
        jButton1.addActionListener(this);
        jButton2.addActionListener(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("word.txt"));
        try {
            for (int i = 0; i < strings.length; i++) {
                strings[i] = f.readLine();//每次读取一行信息
            }
            //四个单词的信息分别放在四个数组中
            word1 = strings[0].split(" ");
            word2 = strings[1].split(" ");
            word3 = strings[2].split(" ");
            word4 = strings[3].split(" ");
            //将四个单词数组存放在集合中
            arrayList.add(word1);
            arrayList.add(word2);
            arrayList.add(word3);
            arrayList.add(word4);
        } catch (IOException e) {
            e.printStackTrace();//打印异常报错及原因
        }
        return arrayList;
    }
    //获取随机单词
    public void getRandomStringArrays(int i) throws FileNotFoundException {
        arrayList = getWord();
        str = arrayList.get(i);
    }
    //第n次正确后得答对题数
    public String getLanel() {
        return "答对单词数:" + m + "/4";
    }

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

  • 11
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白茶..

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值