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
- Java小程序2数学题练习小程序
- 练习小程序2
- Java基础练习小程序
- 练习小程序1
- 小程序练习
- ibatis练习小程序
- 编程练习小程序
- 练习小程序
- 练习小程序
- 代码小程序练习
- 练习小程序
- python练习小程序
- python小程序练习
- 黑马程序员---java小程序练习
- java小程序2
- Object-C练习小程序(2)
- c语言小程序练习
- C++入门小程序练习
- Maven学习 (一) 搭建Maven环境
- 微软句向量工具包Sent2vec
- Volley执行耗时操作
- ABAP 字符串转日期函数
- ftp文件上传实例
- Java小程序2数学题练习小程序
- java中Thread用法
- Maven学习 (二) Eclipse 上安装 Maven3插件
- java抽象类
- ora-01410 无效的rowid
- 编译boost程序出现如下错误fatal error LNK1104: cannot open file 'libboost_system-vc100-mt-gd-1_54.lib'的解决方法
- Posix线程编程指南(1)
- Web Service进阶(六)SOAPBinding绑定方式异常 is not found. Have you run APT to generate them
- Swift语言指南(二)基础操作符