Java小程序2数学题练习小程序

来源:互联网 发布:cf极光和毁灭数据对比 编辑:程序博客网 时间:2024/05/30 04:09

编写一个Java小程序,在文本框中显示出一道数学题,答对后打印在文本区域中,答错打出提示,一共10道

//MathProblem.java 主类import java.awt.*;import java.awt.event.*;import javax.swing.JTextField;public class MathProblem extends Frame implements ActionListener {    private static final long serialVersionUID = 1L;    TextArea ta;    JTextField tf1,tf2;    Label lb1,lb2;    Button bt1,bt2,bt3;    Panel p1,p2,p3,p4,p5,p6;    Frame f;    int i;    Question qu=new Question();    MathProblem(){        i=0;        ta=new TextArea();        ta.setSize(480, 180);        ta.setEditable(true);        tf1=new JTextField(20);        tf2=new JTextField(10);        tf1.setHorizontalAlignment(JTextField.CENTER);        tf1.setText(qu.math[i]);        tf1.setEditable(true);        tf2.setEditable(true);        lb1=new Label("题目:");        lb2=new Label("答案:");        bt1=new Button("确定");        bt2=new Button("上一题");        bt3=new Button("下一题");        bt1.addActionListener(this);        bt2.addActionListener(this);        bt3.addActionListener(this);        p1=new Panel();        p2=new Panel();        p3=new Panel();        p1.add(ta);        p2.add(lb1);        p2.add(tf1);        p2.add(lb2);        p2.add(tf2);        p3.setLayout(new GridLayout(1,3));        p3.add(bt1);        p3.add(bt2);        p3.add(bt3);        f=new Frame("练习题");        f.setVisible(true);        f.setResizable(false);        f.setSize(500, 280);        f.setLocationRelativeTo(null);        f.add(p1,BorderLayout.NORTH);        f.add(p2,BorderLayout.CENTER);        f.add(p3,BorderLayout.SOUTH);        f.addWindowListener(new WindowAdapter(){            public void windowClosing(WindowEvent e){                System.exit(0);            }        });    }    public void actionPerformed(ActionEvent e) {        if(e.getSource().equals(bt1)){            if(tf2.getText().equals(qu.mathKey[i])){                ta.setText(qu.math[i]+qu.mathKey[i]);            }            else{                ta.setText("error!!!");            }        }        if(e.getSource().equals(bt2)){            i--;            if(i>=0){                tf1.setText(qu.math[i]);                tf2.setText(null);                ta.setText(null);            }            else{                ta.setText("已经是第一题!");                i=0;            }        }        if(e.getSource().equals(bt3)){            i++;            if(i<=9){                tf1.setText(qu.math[i]);                tf2.setText(null);                ta.setText(null);            }            else{                ta.setText("已经是最后一题!");                i=9;            }        }    }    public static void main(String args[]){        MathProblem mp=new MathProblem();    }}//Question.java 该类主要存放数组用于放置题目和答案public class Question {    String[] math;    String mathKey[]={"2","4","8","2","7","45","4","124","132","1"};    Question(){        math=new String[10];        math[0]="1+1=";        math[1]="10-6=";        math[2]="2*4=";        math[3]="6÷3=";        math[4]="1+2*3=";        math[5]="(100-10)*2=";        math[6]="(7+1)÷2=";        math[7]="1+123=";        math[8]="64÷2+100=";        math[9]="8+1-8=";    }}

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

0 0
原创粉丝点击