java做的简单的数学测试题
2012-06-01 16:02
225 查看
仅用于保存自己的代码,同时也欢迎各位提出批评建议,以完善代码。
1. Mytest.java(主界面)
package school;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.Random;
public class Mytest extends JFrame implements ActionListener{
JFrame f1,f2,f3,f4;
JRadioButton r1,r2,r3;
JButton ok;
JTextField t1,t2,t3,t4,t5;
JLabel la;
Mytest(){
f1=new JFrame("学生测试系统");
r1=new JRadioButton("简单");
r2=new JRadioButton("中等");
r3=new JRadioButton("高级");
ok=new JButton("确定");
f1.setVisible(true);
f1.add(r1);
f1.add(r2);
f1.add(r3);
f1.add(ok);
f1.setLayout(new GridLayout(4,1));
ok.addActionListener(this);
r1.addActionListener(this);
r2.addActionListener(this);
r3.addActionListener(this);
f1.setSize(300,300);
}
public int myrnd(){
Random ran=new Random();
int i=ran.nextInt(51);
return i;
}
public void actionPerformed(ActionEvent e) {
f1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭窗体后释放资源,若不写这条语句,
//则虽然关闭了窗体,但进程还在运行。
if(e.getSource()==r1){
Mytest2 tt=new Mytest2();
tt.f2.setVisible(true);//调用另外一个类:Mytest2的窗体
}
if(e.getSource()==r2){
Mytest3 t3=new Mytest3();
t3.f3.setVisible(true);
}
if(e.getSource()==r3){
Mytest4 t4=new Mytest4();
t4.f4.setVisible(true);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Mytest();
Font f = new Font("楷书",Font.BOLD,20); //设置label控件的字体
UIManager.put("Label.font",f);
}
}
2 Mytest2.java
package school;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.Random;
public class Mytest2 extends JFrame implements ActionListener{
JFrame f2;
JButton kk,sure;
JTextField t1,t2,t3,t4,t5,t6,t7;
JTextField p[];
JLabel la1,rig1,pg[],rgval[],total,score,bb[][];
public Mytest2(){
f2=new JFrame("低年级数学测试题");
f2.setVisible(true);
total=new JLabel("总分为:");
score=new JLabel("");
score.setSize(150, 120);
Font f = new Font("楷书",Font.BOLD,22); //设置label控件的字体
UIManager.put("Label.font",f);
Font font=new Font("宋体",Font.BOLD,20);//设置文本框的字体大小
UIManager.put("TextField.font",font);
kk=new JButton("确定");
kk.addActionListener(this);//kk事件
bb=new JLabel[10][4];//创建对象bb
p=new JTextField[10];
for(int i=0;i<10;i++){ //初始化p[i]文本框
p[i]=new JTextField(50);
}
for(int i=0;i<10;i++){ //10行4列的文本框
for(int j=0;j<4;j++){ //初始化二维数组aa
bb[i][j]=new JLabel("");
bb[i][j].setSize(10,10);
}
}
pg=new JLabel[10];
rgval=new JLabel[10];
for(int i=0;i<10;i++)//初始化两个label控件数组
{
pg[i]=new JLabel("");
rgval[i]=new JLabel("");
}
sure=new JButton("交卷");
int a[],b[];
a=new int[6];
b=new int[6];
for(int i=0;i<6;i++){
a[i]=myrnd();//运算符左边的随机数
b[i]=myrnd();//运算符右边的随机数
}
for(int i=0;i<6;i++){
bb[i][0].setText(String.valueOf(a[i]));//第一列文本框的值
bb[i][2].setText(String.valueOf(b[i]));//第三列文本框的值
}
int c[]=new int[4];
int d[]=new int[4];
for(int i=0;i<4;i++)
{
c[i]=rnd();
d[i]=rnd();
}
for(int i=0;i<4;i++){
bb[i+6][0].setText(String.valueOf(c[i]));
bb[i+6][2].setText(String.valueOf(d[i]));
}
for(int i=0;i<10;i++){
bb[i][3].setText("=");
}
for(int i=0;i<3;i++){
bb[i][1].setText("+");
bb[i+3][1].setText("-");
}
for(int i=6;i<10;i++)
bb[i][1].setText("×");
int rn[]=new int[10];//这个数组用于存储正确答案
for(int i=0;i<3;i++)
rn[i]=a[i]+b[i];
for(int i=3;i<6;i++)
rn[i]=a[i]-b[i];
for(int i=0;i<4;i++)
rn[i+6]=c[i]*d[i];
for(int i=0;i<10;i++){//将正确答案存入rgval这个label控件中,并设置为不可见的
rgval[i].setText(String.valueOf(rn[i]));
rgval[i].setVisible(false);
}
String user[]=new String[10];//这个数组用于存储用户输入的答案
for(int i=0;i<10;i++)
user[i]=p[i].getText();
for(int i=0;i<10;i++){
for(int j=0;j<4;j++){
f2.add(bb[i][j]);
}
f2.add(p[i]);
f2.add(pg[i]);
f2.add(rgval[i]);
}
f2.add(sure);f2.add(total);f2.add(score);
f2.setLayout(new GridLayout(12,7));
f2.setSize(1300,1100);
sure.addActionListener(this);
}
public int myrnd(){
Random ran=new Random();
int i=ran.nextInt(71);//产生0到70的随机数
return i;
}
public int rnd(){
Random ran=new Random();
int i=ran.nextInt(10);//产生0到9的随机数
return i;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==sure){
int count=0;
for(int i=0;i<10;i++){
if(p[i].getText().equals(rgval[i].getText())){
pg[i].setText("√");
count=count+1;
pg[i].setVisible(true);
pg[i].setForeground(Color.red);//设置字体颜色
}
else{
pg[i].setText("×");
pg[i].setForeground(Color.red);
rgval[i].setText("正确答案是:"+rgval[i].getText());
rgval[i].setVisible(true);
rgval[i].setForeground(Color.red);
}
}
count=count*10;
switch (count){
case 100:
score.setText(String.valueOf(count)+" "+"满分,恭喜你!不要骄傲哦!");
score.setForeground(Color.red);
break;
case 90:
score.setText(String.valueOf(count)+"分 "+" "+"不要马虎哦!");
score.setForeground(Color.red);
break;
case 80:
score.setText(String.valueOf(count)+"分 "+" "+"要继续努力哦!");
score.setForeground(Color.red);
break;
default:
score.setText(String.valueOf(count)+" "+"不行啊,赶紧加油,多看书!");
score.setForeground(Color.red);
}
}
}
}
3 Mytest3.java
package school;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.*;
import java.util.List;
public class Mytest3 extends JFrame implements ActionListener{
JFrame f3;
JButton ok;
JLabel lb[][],pg[],total,score,rg[];
JTextField tt[];
byte[] yy=new byte[10];
public Mytest3(){
f3=new JFrame("中年级数学测试题");
f3.setVisible(true);
total=new JLabel("总分");
score=new JLabel("");
score.setSize(150, 120);
Font f = new Font("楷书",Font.BOLD,20); //设置label控件的字体
UIManager.put("Label.font",f);
Font font=new Font("宋体",Font.BOLD,20);//设置文本框的字体大小
UIManager.put("TextField.font",font);
lb=new JLabel[10][4]; //lb数组用于存储一个10行4列的数组
tt=new JTextField[10]; //tt用于存储文本框中用户输入的结果
for(int i=0;i<10;i++){ //初始化tt[i]文本框
tt[i]=new JTextField(50);
}
for(int i=0;i<10;i++){ //10行4列的标签数组
for(int j=0;j<4;j++){ //初始化二维数组lb
lb[i][j]=new JLabel("");
lb[i][j].setSize(10,10);
}
}
pg=new JLabel[10];
rg=new JLabel[10];
for(int i=0;i<10;i++)//初始化两个label控件数组
{
pg[i]=new JLabel("");
rg[i]=new JLabel("");
}
ok=new JButton("交卷");
int a[],b[];
a=new int[4];
b=new int[4];
for(int i=0;i<4;i++){
a[i]=myrnd();//运算符左边的随机数
b[i]=myrnd();//运算符右边的随机数
}
for(int i=0;i<4;i++){
lb[i][0].setText(String.valueOf(a[i]));//第一列文本框的值
lb[i][2].setText(String.valueOf(b[i]));//第三列文本框的值
}
int c[]=new int[3];
int d[]=new int[3];
for(int i=0;i<3;i++)
{
c[i]=rnd();
d[i]=rnd();
}
for(int i=0;i<3;i++){
lb[i+4][0].setText(String.valueOf(c[i]));
lb[i+4][2].setText(String.valueOf(d[i]));
}
int div1[]=new int [3];
int div2[]=new int[3];
int pp[]=new int[3];
for(int i=0;i<3;i++)
{
div1[i]=ddrnd();
for(int r=0;r<200;r++){
pp[i]=ddrnd();
if( (pp[i]!=0) && (div1[i]%pp[i]==0)&&div1[i]!=0){ //让除数是被除数的整数倍,并且除数不为0.
div2[i]=pp[i];
break;
}
else
continue;
}
}
for(int i=0;i<3;i++)
{
lb[i+7][0].setText(String.valueOf(div1[i]));
lb[i+7][2].setText(String.valueOf(div2[i]));
}
for(int i=0;i<10;i++){
lb[i][3].setText("=");
}
int rgack[]=new int[10];//这个数组用来存储正确答案
for(int i=0;i<2;i++)
{
lb[i][1].setText("+");
rgack[i]=a[i]+b[i];
lb[i+2][1].setText("-");
rgack[i+2]=a[i+2]-b[i+2];
}
for(int i=0;i<3;i++){
lb[i+4][1].setText("×");
rgack[i+4]=c[i]*d[i];
lb[i+7][1].setText("÷");
rgack[i+7]=div1[i]/div2[i];
}
for(int i=0;i<10;i++){//将正确答案存入rg这个label控件中,并设置为不可见的
rg[i].setText(String.valueOf(rgack[i]));
rg[i].setVisible(false);
}
String user[]=new String[10];//这个数组用于存储用户输入的答案
for(int i=0;i<10;i++)
user[i]=tt[i].getText();
for(int i=0;i<10;i++){
for(int j=0;j<4;j++){
f3.add(lb[i][j]);
}
f3.add(tt[i]);
f3.add(pg[i]);
f3.add(rg[i]);
}
f3.add(ok);
f3.add(total);
f3.add(score);
f3.setLayout(new GridLayout(11,7));
f3.setSize(1300,1100);
ok.addActionListener(this);
}
private Object Integer(Object object) {
// TODO Auto-generated method stub
return null;
}
public int myrnd(){
Random ran=new Random();
int i=ran.nextInt(501);//产生0到500的随机数
return i;
}
public int rnd(){
Random ran=new Random();
int i=ran.nextInt(51);//产生0到50的随机数
return i;
}
public int ddrnd(){
int i=(int) (Math.random()*100+1);//产生1到100的随机数
return i;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==ok){
int count=0;
for(int i=0;i<10;i++){
if(tt[i].getText().equals(rg[i].getText())){
pg[i].setText("√");
count=count+1;
pg[i].setVisible(true);
pg[i].setForeground(Color.red);//设置字体颜色
}
else{
pg[i].setText("×");
pg[i].setForeground(Color.red);
rg[i].setText("正确答案是:"+rg[i].getText());
rg[i].setVisible(true);
rg[i].setForeground(Color.red);
}
}
count=count*10;
switch (count){
case 100:
score.setText(String.valueOf(count)+" "+"满分,恭喜你!不要骄傲哦!");
score.setForeground(Color.red);
break;
case 90:
score.setText(String.valueOf(count)+"分 "+" "+"不要马虎哦!");
score.setForeground(Color.red);
break;
case 80:
score.setText(String.valueOf(count)+"分 "+" "+"要继续努力哦!");
score.setForeground(Color.red);
break;
case 70:
score.setText(String.valueOf(count)+"分 "+" "+"要多多加油努力哦!");
score.setForeground(Color.red);
break;
case 60:
score.setText(String.valueOf(count)+"分 "+" "+"要多多加油努力哦!");
score.setForeground(Color.red);
break;
default:
score.setText(String.valueOf(count)+" "+"不行啊,赶紧加油,多看书!");
score.setForeground(Color.red);
}
}
}
}
4 Mytest4.java
package school;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import school.Timef4.Mytimes;
import java.math.BigDecimal;
import java.util.*;
public class Mytest4 extends JFrame implements ActionListener {
JFrame f4;
static JLabel mark1,mark2,marktime;
static JLabel lab1;
static JLabel lab2;
static JLabel laboper1;
static JLabel laboper2;
static JLabel pg;
static JLabel riganw;
static JTextField jtuser1;
JPanel panel1,panel2;
JButton jb1;
static int count=0;
Mytest4(){
f4=new JFrame("高年级数学测试题");
panel1=new JPanel();
//panel1.setSize(300,300);
panel2=new JPanel();
//panel2.setSize(300,400);
panel1.setPreferredSize(new Dimension(300, 250));//关键代码,设置JPanel的大小
panel2.setPreferredSize(new Dimension(300, 200));//关键代码,设置JPanel的大小
f4.setVisible(true);
mark1=new JLabel("口算题第一题");
mark2=new JLabel("剩余时间:");
marktime=new JLabel("20");
marktime.setSize(10,10);
jb1=new JButton("ok");
jtuser1=new JTextField(20);
lab1=new JLabel("");
lab1.setSize(10,10);
lab2=new JLabel("");
lab2.setSize(10,10);
laboper1=new JLabel("");
laboper1.setSize(10,10);
laboper2=new JLabel("");
laboper2.setSize(10,10);
pg=new JLabel("");
pg.setSize(10,10);
riganw=new JLabel("");
double lf=dbrand();
lab1.setText(String.valueOf(lf));
laboper1.setText("×");
int rf=intersmrand();
lab2.setText(String.valueOf(rf));
laboper2.setText("=");
double p2=lf*rf;
double pp=round(p2,2,BigDecimal.ROUND_HALF_UP);//精确到小数点后两位
riganw.setText(String.valueOf(pp));
riganw.setVisible(false);
panel1.add(mark1);
panel1.add(mark2);
panel1.add(marktime);
panel2.add(lab1);
panel2.add(laboper1);
panel2.add(lab2);
panel2.add(laboper2);
panel2.add(jtuser1);
panel2.add(pg);
panel2.add(riganw);
f4.add(panel1);
f4.add(panel2);
f4.add(jb1);
f4.setLayout(new GridLayout(3,8));
f4.setSize(700,400);
jb1.addActionListener(this);
new Mytimes().start();//另外分配一个线程
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public int interbigrand(){
Random ran=new Random();
int i=ran.nextInt(101);//产生0到100的随机数
return i;
}
public int intersmrand(){
Random ran=new Random();
int i=(int) (Math.random()*9+1);//产生1到9的随机数
return i;
}
public double dbrand(){
double i=Math.random();
return (round(i,2,BigDecimal.ROUND_HALF_UP)); //精确到两位小数
}
public double round(double value, int scale, int roundingMode) {
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(scale, roundingMode);
double d = bd.doubleValue();
bd = null;
return d;
}
class Mytimes extends Thread {
int second=20;
public void run() {
try {
while(second >0) {
try {
Thread.sleep(1000);
second -= 1;//时间减去一秒
second=((int)second % 60);
marktime.setText(String.format("%02d",second));
System.out.println(String.format("%02d",second));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(second== 0){
marktime.setText(String.format("%02d",second));
if(jtuser1.getText()==null)
jtuser1.setText("null1");
Mytest4_2 m3=new Mytest4_2(); //时间到了则打开下一个窗体
m3.f4.setVisible(true);
f4.setVisible(false);//关闭当前窗体
System.out.println("game over!");
}
} catch (Exception e) {
}
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jb1 ){
if(jtuser1.getText().equals(riganw.getText())){
pg.setText("√");
count=count+1;
pg.setVisible(true);
pg.setForeground(Color.red);//设置字体颜色
}
else{
pg.setText("×");
pg.setForeground(Color.red);
riganw.setText("正确答案是:"+riganw.getText());
riganw.setVisible(true);
riganw.setForeground(Color.red);
}
}
}
}
Mytest4_2.java
package school;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.math.BigDecimal;
import java.util.*;
public class Mytest4_2 extends JFrame implements ActionListener {
JFrame f4;
static JLabel mark1,mark2,marktime,lab1,lab2,laboper1,laboper2,pg,riganw;
static JTextField jtuser1;
JPanel panel1,panel2;
JButton jb1;
static int count=Mytest4.count;
Mytest4_2(){
f4=new JFrame("高年级数学测试题");
panel1=new JPanel();
panel2=new JPanel();
panel1.setPreferredSize(new Dimension(300, 250));//关键代码,设置JPanel的大小
panel2.setPreferredSize(new Dimension(300, 200));//关键代码,设置JPanel的大小
f4.setVisible(true);
mark1=new JLabel("口算题第二题 ");
mark2=new JLabel("剩余时间:");
marktime=new JLabel("20");
marktime.setSize(10,10);
jb1=new JButton("ok");
jtuser1=new JTextField(20);
lab1=new JLabel("");
lab1.setSize(10,10);
lab2=new JLabel("");
lab2.setSize(10,10);
laboper1=new JLabel("");
laboper1.setSize(10,10);
laboper2=new JLabel("");
laboper2.setSize(10,10);
pg=new JLabel("");
pg.setSize(10,10);
riganw=new JLabel("");
RandMytest rmt=new RandMytest(); //调用另一个类中的rand函数
double lf=rmt.dbrand();
lab1.setText(String.valueOf(lf));
laboper1.setText("×");
int rf=rmt.intersmrand();
lab2.setText(String.valueOf(rf));
laboper2.setText("=");
double p2=lf*rf;
double pp=rmt.round(p2,2,BigDecimal.ROUND_HALF_UP);//精确到小数点后两位
riganw.setText(String.valueOf(pp));
riganw.setVisible(false);
panel1.add(mark1);
panel1.add(mark2);
panel1.add(marktime);
panel2.add(lab1);
panel2.add(laboper1);
panel2.add(lab2);
panel2.add(laboper2);
panel2.add(jtuser1);
panel2.add(pg);
panel2.add(riganw);
f4.add(panel1);
f4.add(panel2);
f4.add(jb1);
f4.setLayout(new GridLayout(3,8));
f4.setSize(700,400);
jb1.addActionListener(this);
// //另外分配一个线程
new Mytimes().start();
setDefaultCloseOperation(EXIT_ON_CLOSE);
if(jtuser1.getText()==null)
jtuser1.setText("null1");
}
/*
public int interbigrand(){
Random ran=new Random();
int i=ran.nextInt(101);//产生0到100的随机数
return i;
}
public int intersmrand(){
Random ran=new Random();
int i=(int) (Math.random()*9+1);//产生1到9的随机数
return i;
}
public double dbrand(){
double i=Math.random();
return (round(i,2,BigDecimal.ROUND_HALF_UP)); //精确到两位小数
}
public double round(double value, int scale, int roundingMode) {
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(scale, roundingMode);
double d = bd.doubleValue();
bd = null;
return d;
}
class Mytimes extends Thread {
int second=20;
public void run() {
try {
while(second >0) {
try {
Thread.sleep(1000);
second -= 1;//时间减去一秒
second=((int)second % 60);
marktime.setText(String.format("%02d",second));
System.out.println(String.format("%02d",second));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(second== 0){
marktime.setText(String.format("%02d",second));
if(jtuser1.getText()==null)
jtuser1.setText("null");
Mytest4_3 m3=new Mytest4_3(); //时间到了则打开下一个窗体
m3.f4.setVisible(true);
f4.setVisible(false);//关闭当前窗体
System.out.println("game over!");
}
} catch (Exception e) {
}
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jb1 ){
if(jtuser1.getText().equals(riganw.getText())){
pg.setText("√");
count=count+1;
pg.setVisible(true);
pg.setForeground(Color.red);//设置字体颜色
}
else{
pg.setText("×");
pg.setForeground(Color.red);
riganw.setText("正确答案是:"+riganw.getText());
riganw.setVisible(true);
riganw.setForeground(Color.red);
}
}
}
}
Mytest5_2.java(最后一题,做完后分数汇总在消息框)
package school;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.math.BigDecimal;
import java.util.*;
public class Mytest5_2 extends JFrame implements ActionListener {
JFrame f4;
JLabel mark1,mark2,marktime,lab1,lab2,laboper1,lab4,lab5,pg,riganw1,riganw2;
JTextField jtuser1,jtuser2;
JPanel panel1,panel2;
JButton jb1;
static int count=Mytest5.count;//用来计算答对的题目个数
static int score=0;
Mytest5_2(){
f4=new JFrame("高年级数学测试题");
panel1=new JPanel();
panel2=new JPanel();
panel1.setPreferredSize(new Dimension(300, 250));//关键代码,设置JPanel的大小
panel2.setPreferredSize(new Dimension(300, 250));//关键代码,设置JPanel的大小
f4.setVisible(true);
mark1=new JLabel("快速笔算题 ");
mark2=new JLabel("剩余时间:");
marktime=new JLabel("60");
marktime.setSize(10,10);
lab4=new JLabel("的最大公约数是");
lab5=new JLabel("最小公倍数是");
jb1=new JButton("ok");
jtuser1=new JTextField(5);
jtuser2=new JTextField(5);
lab1=new JLabel("");
lab1.setSize(10,10);
lab2=new JLabel("");
lab2.setSize(10,10);
laboper1=new JLabel("和");
pg=new JLabel("");
pg.setSize(10,10);
riganw1=new JLabel("");
riganw2=new JLabel("");
RandMytest rmt=new RandMytest(); //调用另一个类中的rand函数
int lf1=rmt.inte150();
int lf2=rmt.inte150();
lab1.setText(String.valueOf(lf1));
lab2.setText(String.valueOf(lf2));
BigDivisor bd=new BigDivisor();
int p2=bd.BigDivisor(lf1, lf2);
int p3=(lf1*lf2)/p2;
riganw1.setText(String.valueOf(p2));
riganw2.setText(String.valueOf(p3));
riganw1.setVisible(false);
riganw2.setVisible(false);
panel1.add(mark1);
panel1.add(mark2);
panel1.add(marktime);
panel2.add(lab1);
panel2.add(laboper1);
panel2.add(lab2);
panel2.add(lab4);
panel2.add(jtuser1);
panel2.add(lab5);
panel2.add(jtuser2);
panel2.add(pg);
panel2.add(riganw1);
panel2.add(riganw2);
f4.add(panel1);
f4.add(panel2);
f4.add(jb1);
f4.setLayout(new GridLayout(3,12));
f4.setSize(800,500);
jb1.addActionListener(this);
// //另外分配一个线程
new Mytimes().start();
setDefaultCloseOperation(EXIT_ON_CLOSE);
if(jtuser1.getText()==null)
jtuser1.setText("null");
}
class Mytimes extends Thread {
int second=60;
public void run() {
try {
while(second >0) {
try {
Thread.sleep(1000);
second -= 1;//时间减去一秒
// second=((int)second % 60);
marktime.setText(String.format("%02d",second));
System.out.println(String.format("%02d",second));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(second== 0){
marktime.setText(String.format("%02d",second));
if(jtuser1.getText()==null)
jtuser1.setText("null1");
if(jtuser2.getText()==null)
jtuser2.setText("null2");
System.out.println("game over!");
}
} catch (Exception e) {
}
}
}
public void actionPerformed(ActionEvent e) {
f4.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭窗体后释放资源,若不写这条语句,
//则虽然关闭了窗体,但进程还在运行。
if(e.getSource()==jb1 ){
if(jtuser1.getText().equals(riganw1.getText())&&jtuser2.getText().equals(riganw2.getText())){
pg.setText("√");
count=count+1;
score=count*10;
pg.setVisible(true);
pg.setForeground(Color.red);//设置字体颜色
}
else{
pg.setText("×");
pg.setForeground(Color.red);
riganw1.setText("正确答案是:"+riganw1.getText());
riganw1.setVisible(true);
riganw1.setForeground(Color.red);
riganw2.setText("最小公倍数是:"+riganw2.getText());
riganw2.setVisible(true);
riganw2.setForeground(Color.red);
}
javax.swing.JOptionPane.showMessageDialog(this,"**题目********你的答案**************************正确答案*****"+"\\\n"
+Mytest4.lab1.getText()+Mytest4.laboper1.getText()+Mytest4.lab2.getText()+Mytest4.laboper2.getText()+Mytest4.jtuser1.getText()+"***********************************"+Mytest4.riganw.getText()+"\\\n"
+Mytest4_2.lab1.getText()+Mytest4_2.laboper1.getText()+Mytest4_2.lab2.getText()+Mytest4_2.laboper2.getText()+Mytest4_2.jtuser1.getText()+"******************************************"+Mytest4_2.riganw.getText()+"\\\n"
+Mytest4_3.lab1.getText()+Mytest4_3.laboper1.getText()+Mytest4_3.lab2.getText()+Mytest4_3.laboper2.getText()+Mytest4_3.jtuser1.getText()+"*************************************************"+Mytest4_3.riganw.getText()+"\\\n"
+Mytest4_4.lab1.getText()+Mytest4_4.laboper1.getText()+Mytest4_4.lab2.getText()+Mytest4_4.laboper2.getText()+Mytest4_4.jtuser1.getText()+"**************************************************"+Mytest4_4.riganw.getText()+"\\\n"
+Mytest4_5.lab1.getText()+Mytest4_5.lab5.getText()+Mytest4_5.lab2.getText()+Mytest4_5.laboper1.getText()+Mytest4_5.lab3.getText()+Mytest4_5.lab6.getText()+Mytest4_5.lab4.getText()+Mytest4_5.laboper2.getText()+Mytest4_5.jtuser1.getText()+Mytest4_5.laboper3.getText()+Mytest4_5.jtuser2.getText()+"****正确结果******"+Mytest4_5.riganw1.getText()+Mytest4_5.laboper3.getText()+Mytest4_5.riganw2.getText()+"\\\n"
+Mytest4_6.lab1.getText()+Mytest4_6.lab5.getText()+Mytest4_6.lab2.getText()+Mytest4_6.laboper1.getText()+Mytest4_6.lab3.getText()+Mytest4_6.lab6.getText()+Mytest4_6.lab4.getText()+Mytest4_6.laboper2.getText()+Mytest4_6.jtuser1.getText()+Mytest4_6.laboper3.getText()+Mytest4_6.jtuser2.getText()+"*******正确结果*******"+Mytest4_6.riganw1.getText()+"/"+Mytest4_6.riganw2.getText()+"\\\n"
+Mytest4_7.lab1.getText()+Mytest4_7.lab5.getText()+Mytest4_7.lab2.getText()+Mytest4_7.laboper1.getText()+Mytest4_7.lab3.getText()+Mytest4_7.lab6.getText()+Mytest4_7.lab4.getText()+Mytest4_7.laboper2.getText()+Mytest4_7.jtuser1.getText()+Mytest4_7.laboper3.getText()+Mytest4_7.jtuser2.getText()+"******正确结果********"+Mytest4_7.riganw1.getText()+"/"+Mytest4_7.riganw2.getText()+"\\\n"
+Mytest4_8.lab1.getText()+Mytest4_8.lab5.getText()+Mytest4_8.lab2.getText()+Mytest4_8.laboper1.getText()+Mytest4_8.lab3.getText()+Mytest4_8.lab6.getText()+Mytest4_8.lab4.getText()+Mytest4_8.laboper2.getText()+Mytest4_8.jtuser1.getText()+Mytest4_8.laboper3.getText()+Mytest4_8.jtuser2.getText()+"******正确结果******"+Mytest4_8.riganw1.getText()+"和"+Mytest4_8.riganw2.getText()+"\\\n"
+Mytest5.lab1.getText()+Mytest5.laboper1.getText()+Mytest5.lab2.getText()+Mytest5.laboper2.getText()+Mytest5.lab3.getText()+Mytest5.lab4.getText()+Mytest5.jtuser1.getText()+","+Mytest5.lab5.getText()+Mytest5.jtuser2.getText()+"******正确结果******"+Mytest5.riganw1.getText()+"
和 "+Mytest5.riganw2.getText()+"\\\n"
+lab1.getText()+laboper1.getText()+lab2.getText()+lab4.getText()+jtuser1.getText()+","+lab5.getText()+jtuser2.getText()+"********正确答案********"+riganw1.getText()+" **和** "+riganw2.getText()+"\\\n"
+"你的总分是:"+Mytest5_2.score);
}
}
}
1. Mytest.java(主界面)
package school;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.Random;
public class Mytest extends JFrame implements ActionListener{
JFrame f1,f2,f3,f4;
JRadioButton r1,r2,r3;
JButton ok;
JTextField t1,t2,t3,t4,t5;
JLabel la;
Mytest(){
f1=new JFrame("学生测试系统");
r1=new JRadioButton("简单");
r2=new JRadioButton("中等");
r3=new JRadioButton("高级");
ok=new JButton("确定");
f1.setVisible(true);
f1.add(r1);
f1.add(r2);
f1.add(r3);
f1.add(ok);
f1.setLayout(new GridLayout(4,1));
ok.addActionListener(this);
r1.addActionListener(this);
r2.addActionListener(this);
r3.addActionListener(this);
f1.setSize(300,300);
}
public int myrnd(){
Random ran=new Random();
int i=ran.nextInt(51);
return i;
}
public void actionPerformed(ActionEvent e) {
f1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭窗体后释放资源,若不写这条语句,
//则虽然关闭了窗体,但进程还在运行。
if(e.getSource()==r1){
Mytest2 tt=new Mytest2();
tt.f2.setVisible(true);//调用另外一个类:Mytest2的窗体
}
if(e.getSource()==r2){
Mytest3 t3=new Mytest3();
t3.f3.setVisible(true);
}
if(e.getSource()==r3){
Mytest4 t4=new Mytest4();
t4.f4.setVisible(true);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Mytest();
Font f = new Font("楷书",Font.BOLD,20); //设置label控件的字体
UIManager.put("Label.font",f);
}
}
2 Mytest2.java
package school;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.Random;
public class Mytest2 extends JFrame implements ActionListener{
JFrame f2;
JButton kk,sure;
JTextField t1,t2,t3,t4,t5,t6,t7;
JTextField p[];
JLabel la1,rig1,pg[],rgval[],total,score,bb[][];
public Mytest2(){
f2=new JFrame("低年级数学测试题");
f2.setVisible(true);
total=new JLabel("总分为:");
score=new JLabel("");
score.setSize(150, 120);
Font f = new Font("楷书",Font.BOLD,22); //设置label控件的字体
UIManager.put("Label.font",f);
Font font=new Font("宋体",Font.BOLD,20);//设置文本框的字体大小
UIManager.put("TextField.font",font);
kk=new JButton("确定");
kk.addActionListener(this);//kk事件
bb=new JLabel[10][4];//创建对象bb
p=new JTextField[10];
for(int i=0;i<10;i++){ //初始化p[i]文本框
p[i]=new JTextField(50);
}
for(int i=0;i<10;i++){ //10行4列的文本框
for(int j=0;j<4;j++){ //初始化二维数组aa
bb[i][j]=new JLabel("");
bb[i][j].setSize(10,10);
}
}
pg=new JLabel[10];
rgval=new JLabel[10];
for(int i=0;i<10;i++)//初始化两个label控件数组
{
pg[i]=new JLabel("");
rgval[i]=new JLabel("");
}
sure=new JButton("交卷");
int a[],b[];
a=new int[6];
b=new int[6];
for(int i=0;i<6;i++){
a[i]=myrnd();//运算符左边的随机数
b[i]=myrnd();//运算符右边的随机数
}
for(int i=0;i<6;i++){
bb[i][0].setText(String.valueOf(a[i]));//第一列文本框的值
bb[i][2].setText(String.valueOf(b[i]));//第三列文本框的值
}
int c[]=new int[4];
int d[]=new int[4];
for(int i=0;i<4;i++)
{
c[i]=rnd();
d[i]=rnd();
}
for(int i=0;i<4;i++){
bb[i+6][0].setText(String.valueOf(c[i]));
bb[i+6][2].setText(String.valueOf(d[i]));
}
for(int i=0;i<10;i++){
bb[i][3].setText("=");
}
for(int i=0;i<3;i++){
bb[i][1].setText("+");
bb[i+3][1].setText("-");
}
for(int i=6;i<10;i++)
bb[i][1].setText("×");
int rn[]=new int[10];//这个数组用于存储正确答案
for(int i=0;i<3;i++)
rn[i]=a[i]+b[i];
for(int i=3;i<6;i++)
rn[i]=a[i]-b[i];
for(int i=0;i<4;i++)
rn[i+6]=c[i]*d[i];
for(int i=0;i<10;i++){//将正确答案存入rgval这个label控件中,并设置为不可见的
rgval[i].setText(String.valueOf(rn[i]));
rgval[i].setVisible(false);
}
String user[]=new String[10];//这个数组用于存储用户输入的答案
for(int i=0;i<10;i++)
user[i]=p[i].getText();
for(int i=0;i<10;i++){
for(int j=0;j<4;j++){
f2.add(bb[i][j]);
}
f2.add(p[i]);
f2.add(pg[i]);
f2.add(rgval[i]);
}
f2.add(sure);f2.add(total);f2.add(score);
f2.setLayout(new GridLayout(12,7));
f2.setSize(1300,1100);
sure.addActionListener(this);
}
public int myrnd(){
Random ran=new Random();
int i=ran.nextInt(71);//产生0到70的随机数
return i;
}
public int rnd(){
Random ran=new Random();
int i=ran.nextInt(10);//产生0到9的随机数
return i;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==sure){
int count=0;
for(int i=0;i<10;i++){
if(p[i].getText().equals(rgval[i].getText())){
pg[i].setText("√");
count=count+1;
pg[i].setVisible(true);
pg[i].setForeground(Color.red);//设置字体颜色
}
else{
pg[i].setText("×");
pg[i].setForeground(Color.red);
rgval[i].setText("正确答案是:"+rgval[i].getText());
rgval[i].setVisible(true);
rgval[i].setForeground(Color.red);
}
}
count=count*10;
switch (count){
case 100:
score.setText(String.valueOf(count)+" "+"满分,恭喜你!不要骄傲哦!");
score.setForeground(Color.red);
break;
case 90:
score.setText(String.valueOf(count)+"分 "+" "+"不要马虎哦!");
score.setForeground(Color.red);
break;
case 80:
score.setText(String.valueOf(count)+"分 "+" "+"要继续努力哦!");
score.setForeground(Color.red);
break;
default:
score.setText(String.valueOf(count)+" "+"不行啊,赶紧加油,多看书!");
score.setForeground(Color.red);
}
}
}
}
3 Mytest3.java
package school;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.*;
import java.util.List;
public class Mytest3 extends JFrame implements ActionListener{
JFrame f3;
JButton ok;
JLabel lb[][],pg[],total,score,rg[];
JTextField tt[];
byte[] yy=new byte[10];
public Mytest3(){
f3=new JFrame("中年级数学测试题");
f3.setVisible(true);
total=new JLabel("总分");
score=new JLabel("");
score.setSize(150, 120);
Font f = new Font("楷书",Font.BOLD,20); //设置label控件的字体
UIManager.put("Label.font",f);
Font font=new Font("宋体",Font.BOLD,20);//设置文本框的字体大小
UIManager.put("TextField.font",font);
lb=new JLabel[10][4]; //lb数组用于存储一个10行4列的数组
tt=new JTextField[10]; //tt用于存储文本框中用户输入的结果
for(int i=0;i<10;i++){ //初始化tt[i]文本框
tt[i]=new JTextField(50);
}
for(int i=0;i<10;i++){ //10行4列的标签数组
for(int j=0;j<4;j++){ //初始化二维数组lb
lb[i][j]=new JLabel("");
lb[i][j].setSize(10,10);
}
}
pg=new JLabel[10];
rg=new JLabel[10];
for(int i=0;i<10;i++)//初始化两个label控件数组
{
pg[i]=new JLabel("");
rg[i]=new JLabel("");
}
ok=new JButton("交卷");
int a[],b[];
a=new int[4];
b=new int[4];
for(int i=0;i<4;i++){
a[i]=myrnd();//运算符左边的随机数
b[i]=myrnd();//运算符右边的随机数
}
for(int i=0;i<4;i++){
lb[i][0].setText(String.valueOf(a[i]));//第一列文本框的值
lb[i][2].setText(String.valueOf(b[i]));//第三列文本框的值
}
int c[]=new int[3];
int d[]=new int[3];
for(int i=0;i<3;i++)
{
c[i]=rnd();
d[i]=rnd();
}
for(int i=0;i<3;i++){
lb[i+4][0].setText(String.valueOf(c[i]));
lb[i+4][2].setText(String.valueOf(d[i]));
}
int div1[]=new int [3];
int div2[]=new int[3];
int pp[]=new int[3];
for(int i=0;i<3;i++)
{
div1[i]=ddrnd();
for(int r=0;r<200;r++){
pp[i]=ddrnd();
if( (pp[i]!=0) && (div1[i]%pp[i]==0)&&div1[i]!=0){ //让除数是被除数的整数倍,并且除数不为0.
div2[i]=pp[i];
break;
}
else
continue;
}
}
for(int i=0;i<3;i++)
{
lb[i+7][0].setText(String.valueOf(div1[i]));
lb[i+7][2].setText(String.valueOf(div2[i]));
}
for(int i=0;i<10;i++){
lb[i][3].setText("=");
}
int rgack[]=new int[10];//这个数组用来存储正确答案
for(int i=0;i<2;i++)
{
lb[i][1].setText("+");
rgack[i]=a[i]+b[i];
lb[i+2][1].setText("-");
rgack[i+2]=a[i+2]-b[i+2];
}
for(int i=0;i<3;i++){
lb[i+4][1].setText("×");
rgack[i+4]=c[i]*d[i];
lb[i+7][1].setText("÷");
rgack[i+7]=div1[i]/div2[i];
}
for(int i=0;i<10;i++){//将正确答案存入rg这个label控件中,并设置为不可见的
rg[i].setText(String.valueOf(rgack[i]));
rg[i].setVisible(false);
}
String user[]=new String[10];//这个数组用于存储用户输入的答案
for(int i=0;i<10;i++)
user[i]=tt[i].getText();
for(int i=0;i<10;i++){
for(int j=0;j<4;j++){
f3.add(lb[i][j]);
}
f3.add(tt[i]);
f3.add(pg[i]);
f3.add(rg[i]);
}
f3.add(ok);
f3.add(total);
f3.add(score);
f3.setLayout(new GridLayout(11,7));
f3.setSize(1300,1100);
ok.addActionListener(this);
}
private Object Integer(Object object) {
// TODO Auto-generated method stub
return null;
}
public int myrnd(){
Random ran=new Random();
int i=ran.nextInt(501);//产生0到500的随机数
return i;
}
public int rnd(){
Random ran=new Random();
int i=ran.nextInt(51);//产生0到50的随机数
return i;
}
public int ddrnd(){
int i=(int) (Math.random()*100+1);//产生1到100的随机数
return i;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==ok){
int count=0;
for(int i=0;i<10;i++){
if(tt[i].getText().equals(rg[i].getText())){
pg[i].setText("√");
count=count+1;
pg[i].setVisible(true);
pg[i].setForeground(Color.red);//设置字体颜色
}
else{
pg[i].setText("×");
pg[i].setForeground(Color.red);
rg[i].setText("正确答案是:"+rg[i].getText());
rg[i].setVisible(true);
rg[i].setForeground(Color.red);
}
}
count=count*10;
switch (count){
case 100:
score.setText(String.valueOf(count)+" "+"满分,恭喜你!不要骄傲哦!");
score.setForeground(Color.red);
break;
case 90:
score.setText(String.valueOf(count)+"分 "+" "+"不要马虎哦!");
score.setForeground(Color.red);
break;
case 80:
score.setText(String.valueOf(count)+"分 "+" "+"要继续努力哦!");
score.setForeground(Color.red);
break;
case 70:
score.setText(String.valueOf(count)+"分 "+" "+"要多多加油努力哦!");
score.setForeground(Color.red);
break;
case 60:
score.setText(String.valueOf(count)+"分 "+" "+"要多多加油努力哦!");
score.setForeground(Color.red);
break;
default:
score.setText(String.valueOf(count)+" "+"不行啊,赶紧加油,多看书!");
score.setForeground(Color.red);
}
}
}
}
4 Mytest4.java
package school;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import school.Timef4.Mytimes;
import java.math.BigDecimal;
import java.util.*;
public class Mytest4 extends JFrame implements ActionListener {
JFrame f4;
static JLabel mark1,mark2,marktime;
static JLabel lab1;
static JLabel lab2;
static JLabel laboper1;
static JLabel laboper2;
static JLabel pg;
static JLabel riganw;
static JTextField jtuser1;
JPanel panel1,panel2;
JButton jb1;
static int count=0;
Mytest4(){
f4=new JFrame("高年级数学测试题");
panel1=new JPanel();
//panel1.setSize(300,300);
panel2=new JPanel();
//panel2.setSize(300,400);
panel1.setPreferredSize(new Dimension(300, 250));//关键代码,设置JPanel的大小
panel2.setPreferredSize(new Dimension(300, 200));//关键代码,设置JPanel的大小
f4.setVisible(true);
mark1=new JLabel("口算题第一题");
mark2=new JLabel("剩余时间:");
marktime=new JLabel("20");
marktime.setSize(10,10);
jb1=new JButton("ok");
jtuser1=new JTextField(20);
lab1=new JLabel("");
lab1.setSize(10,10);
lab2=new JLabel("");
lab2.setSize(10,10);
laboper1=new JLabel("");
laboper1.setSize(10,10);
laboper2=new JLabel("");
laboper2.setSize(10,10);
pg=new JLabel("");
pg.setSize(10,10);
riganw=new JLabel("");
double lf=dbrand();
lab1.setText(String.valueOf(lf));
laboper1.setText("×");
int rf=intersmrand();
lab2.setText(String.valueOf(rf));
laboper2.setText("=");
double p2=lf*rf;
double pp=round(p2,2,BigDecimal.ROUND_HALF_UP);//精确到小数点后两位
riganw.setText(String.valueOf(pp));
riganw.setVisible(false);
panel1.add(mark1);
panel1.add(mark2);
panel1.add(marktime);
panel2.add(lab1);
panel2.add(laboper1);
panel2.add(lab2);
panel2.add(laboper2);
panel2.add(jtuser1);
panel2.add(pg);
panel2.add(riganw);
f4.add(panel1);
f4.add(panel2);
f4.add(jb1);
f4.setLayout(new GridLayout(3,8));
f4.setSize(700,400);
jb1.addActionListener(this);
new Mytimes().start();//另外分配一个线程
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public int interbigrand(){
Random ran=new Random();
int i=ran.nextInt(101);//产生0到100的随机数
return i;
}
public int intersmrand(){
Random ran=new Random();
int i=(int) (Math.random()*9+1);//产生1到9的随机数
return i;
}
public double dbrand(){
double i=Math.random();
return (round(i,2,BigDecimal.ROUND_HALF_UP)); //精确到两位小数
}
public double round(double value, int scale, int roundingMode) {
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(scale, roundingMode);
double d = bd.doubleValue();
bd = null;
return d;
}
class Mytimes extends Thread {
int second=20;
public void run() {
try {
while(second >0) {
try {
Thread.sleep(1000);
second -= 1;//时间减去一秒
second=((int)second % 60);
marktime.setText(String.format("%02d",second));
System.out.println(String.format("%02d",second));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(second== 0){
marktime.setText(String.format("%02d",second));
if(jtuser1.getText()==null)
jtuser1.setText("null1");
Mytest4_2 m3=new Mytest4_2(); //时间到了则打开下一个窗体
m3.f4.setVisible(true);
f4.setVisible(false);//关闭当前窗体
System.out.println("game over!");
}
} catch (Exception e) {
}
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jb1 ){
if(jtuser1.getText().equals(riganw.getText())){
pg.setText("√");
count=count+1;
pg.setVisible(true);
pg.setForeground(Color.red);//设置字体颜色
}
else{
pg.setText("×");
pg.setForeground(Color.red);
riganw.setText("正确答案是:"+riganw.getText());
riganw.setVisible(true);
riganw.setForeground(Color.red);
}
}
}
}
Mytest4_2.java
package school;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.math.BigDecimal;
import java.util.*;
public class Mytest4_2 extends JFrame implements ActionListener {
JFrame f4;
static JLabel mark1,mark2,marktime,lab1,lab2,laboper1,laboper2,pg,riganw;
static JTextField jtuser1;
JPanel panel1,panel2;
JButton jb1;
static int count=Mytest4.count;
Mytest4_2(){
f4=new JFrame("高年级数学测试题");
panel1=new JPanel();
panel2=new JPanel();
panel1.setPreferredSize(new Dimension(300, 250));//关键代码,设置JPanel的大小
panel2.setPreferredSize(new Dimension(300, 200));//关键代码,设置JPanel的大小
f4.setVisible(true);
mark1=new JLabel("口算题第二题 ");
mark2=new JLabel("剩余时间:");
marktime=new JLabel("20");
marktime.setSize(10,10);
jb1=new JButton("ok");
jtuser1=new JTextField(20);
lab1=new JLabel("");
lab1.setSize(10,10);
lab2=new JLabel("");
lab2.setSize(10,10);
laboper1=new JLabel("");
laboper1.setSize(10,10);
laboper2=new JLabel("");
laboper2.setSize(10,10);
pg=new JLabel("");
pg.setSize(10,10);
riganw=new JLabel("");
RandMytest rmt=new RandMytest(); //调用另一个类中的rand函数
double lf=rmt.dbrand();
lab1.setText(String.valueOf(lf));
laboper1.setText("×");
int rf=rmt.intersmrand();
lab2.setText(String.valueOf(rf));
laboper2.setText("=");
double p2=lf*rf;
double pp=rmt.round(p2,2,BigDecimal.ROUND_HALF_UP);//精确到小数点后两位
riganw.setText(String.valueOf(pp));
riganw.setVisible(false);
panel1.add(mark1);
panel1.add(mark2);
panel1.add(marktime);
panel2.add(lab1);
panel2.add(laboper1);
panel2.add(lab2);
panel2.add(laboper2);
panel2.add(jtuser1);
panel2.add(pg);
panel2.add(riganw);
f4.add(panel1);
f4.add(panel2);
f4.add(jb1);
f4.setLayout(new GridLayout(3,8));
f4.setSize(700,400);
jb1.addActionListener(this);
// //另外分配一个线程
new Mytimes().start();
setDefaultCloseOperation(EXIT_ON_CLOSE);
if(jtuser1.getText()==null)
jtuser1.setText("null1");
}
/*
public int interbigrand(){
Random ran=new Random();
int i=ran.nextInt(101);//产生0到100的随机数
return i;
}
public int intersmrand(){
Random ran=new Random();
int i=(int) (Math.random()*9+1);//产生1到9的随机数
return i;
}
public double dbrand(){
double i=Math.random();
return (round(i,2,BigDecimal.ROUND_HALF_UP)); //精确到两位小数
}
public double round(double value, int scale, int roundingMode) {
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(scale, roundingMode);
double d = bd.doubleValue();
bd = null;
return d;
}
class Mytimes extends Thread {
int second=20;
public void run() {
try {
while(second >0) {
try {
Thread.sleep(1000);
second -= 1;//时间减去一秒
second=((int)second % 60);
marktime.setText(String.format("%02d",second));
System.out.println(String.format("%02d",second));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(second== 0){
marktime.setText(String.format("%02d",second));
if(jtuser1.getText()==null)
jtuser1.setText("null");
Mytest4_3 m3=new Mytest4_3(); //时间到了则打开下一个窗体
m3.f4.setVisible(true);
f4.setVisible(false);//关闭当前窗体
System.out.println("game over!");
}
} catch (Exception e) {
}
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jb1 ){
if(jtuser1.getText().equals(riganw.getText())){
pg.setText("√");
count=count+1;
pg.setVisible(true);
pg.setForeground(Color.red);//设置字体颜色
}
else{
pg.setText("×");
pg.setForeground(Color.red);
riganw.setText("正确答案是:"+riganw.getText());
riganw.setVisible(true);
riganw.setForeground(Color.red);
}
}
}
}
Mytest5_2.java(最后一题,做完后分数汇总在消息框)
package school;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.math.BigDecimal;
import java.util.*;
public class Mytest5_2 extends JFrame implements ActionListener {
JFrame f4;
JLabel mark1,mark2,marktime,lab1,lab2,laboper1,lab4,lab5,pg,riganw1,riganw2;
JTextField jtuser1,jtuser2;
JPanel panel1,panel2;
JButton jb1;
static int count=Mytest5.count;//用来计算答对的题目个数
static int score=0;
Mytest5_2(){
f4=new JFrame("高年级数学测试题");
panel1=new JPanel();
panel2=new JPanel();
panel1.setPreferredSize(new Dimension(300, 250));//关键代码,设置JPanel的大小
panel2.setPreferredSize(new Dimension(300, 250));//关键代码,设置JPanel的大小
f4.setVisible(true);
mark1=new JLabel("快速笔算题 ");
mark2=new JLabel("剩余时间:");
marktime=new JLabel("60");
marktime.setSize(10,10);
lab4=new JLabel("的最大公约数是");
lab5=new JLabel("最小公倍数是");
jb1=new JButton("ok");
jtuser1=new JTextField(5);
jtuser2=new JTextField(5);
lab1=new JLabel("");
lab1.setSize(10,10);
lab2=new JLabel("");
lab2.setSize(10,10);
laboper1=new JLabel("和");
pg=new JLabel("");
pg.setSize(10,10);
riganw1=new JLabel("");
riganw2=new JLabel("");
RandMytest rmt=new RandMytest(); //调用另一个类中的rand函数
int lf1=rmt.inte150();
int lf2=rmt.inte150();
lab1.setText(String.valueOf(lf1));
lab2.setText(String.valueOf(lf2));
BigDivisor bd=new BigDivisor();
int p2=bd.BigDivisor(lf1, lf2);
int p3=(lf1*lf2)/p2;
riganw1.setText(String.valueOf(p2));
riganw2.setText(String.valueOf(p3));
riganw1.setVisible(false);
riganw2.setVisible(false);
panel1.add(mark1);
panel1.add(mark2);
panel1.add(marktime);
panel2.add(lab1);
panel2.add(laboper1);
panel2.add(lab2);
panel2.add(lab4);
panel2.add(jtuser1);
panel2.add(lab5);
panel2.add(jtuser2);
panel2.add(pg);
panel2.add(riganw1);
panel2.add(riganw2);
f4.add(panel1);
f4.add(panel2);
f4.add(jb1);
f4.setLayout(new GridLayout(3,12));
f4.setSize(800,500);
jb1.addActionListener(this);
// //另外分配一个线程
new Mytimes().start();
setDefaultCloseOperation(EXIT_ON_CLOSE);
if(jtuser1.getText()==null)
jtuser1.setText("null");
}
class Mytimes extends Thread {
int second=60;
public void run() {
try {
while(second >0) {
try {
Thread.sleep(1000);
second -= 1;//时间减去一秒
// second=((int)second % 60);
marktime.setText(String.format("%02d",second));
System.out.println(String.format("%02d",second));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(second== 0){
marktime.setText(String.format("%02d",second));
if(jtuser1.getText()==null)
jtuser1.setText("null1");
if(jtuser2.getText()==null)
jtuser2.setText("null2");
System.out.println("game over!");
}
} catch (Exception e) {
}
}
}
public void actionPerformed(ActionEvent e) {
f4.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭窗体后释放资源,若不写这条语句,
//则虽然关闭了窗体,但进程还在运行。
if(e.getSource()==jb1 ){
if(jtuser1.getText().equals(riganw1.getText())&&jtuser2.getText().equals(riganw2.getText())){
pg.setText("√");
count=count+1;
score=count*10;
pg.setVisible(true);
pg.setForeground(Color.red);//设置字体颜色
}
else{
pg.setText("×");
pg.setForeground(Color.red);
riganw1.setText("正确答案是:"+riganw1.getText());
riganw1.setVisible(true);
riganw1.setForeground(Color.red);
riganw2.setText("最小公倍数是:"+riganw2.getText());
riganw2.setVisible(true);
riganw2.setForeground(Color.red);
}
javax.swing.JOptionPane.showMessageDialog(this,"**题目********你的答案**************************正确答案*****"+"\\\n"
+Mytest4.lab1.getText()+Mytest4.laboper1.getText()+Mytest4.lab2.getText()+Mytest4.laboper2.getText()+Mytest4.jtuser1.getText()+"***********************************"+Mytest4.riganw.getText()+"\\\n"
+Mytest4_2.lab1.getText()+Mytest4_2.laboper1.getText()+Mytest4_2.lab2.getText()+Mytest4_2.laboper2.getText()+Mytest4_2.jtuser1.getText()+"******************************************"+Mytest4_2.riganw.getText()+"\\\n"
+Mytest4_3.lab1.getText()+Mytest4_3.laboper1.getText()+Mytest4_3.lab2.getText()+Mytest4_3.laboper2.getText()+Mytest4_3.jtuser1.getText()+"*************************************************"+Mytest4_3.riganw.getText()+"\\\n"
+Mytest4_4.lab1.getText()+Mytest4_4.laboper1.getText()+Mytest4_4.lab2.getText()+Mytest4_4.laboper2.getText()+Mytest4_4.jtuser1.getText()+"**************************************************"+Mytest4_4.riganw.getText()+"\\\n"
+Mytest4_5.lab1.getText()+Mytest4_5.lab5.getText()+Mytest4_5.lab2.getText()+Mytest4_5.laboper1.getText()+Mytest4_5.lab3.getText()+Mytest4_5.lab6.getText()+Mytest4_5.lab4.getText()+Mytest4_5.laboper2.getText()+Mytest4_5.jtuser1.getText()+Mytest4_5.laboper3.getText()+Mytest4_5.jtuser2.getText()+"****正确结果******"+Mytest4_5.riganw1.getText()+Mytest4_5.laboper3.getText()+Mytest4_5.riganw2.getText()+"\\\n"
+Mytest4_6.lab1.getText()+Mytest4_6.lab5.getText()+Mytest4_6.lab2.getText()+Mytest4_6.laboper1.getText()+Mytest4_6.lab3.getText()+Mytest4_6.lab6.getText()+Mytest4_6.lab4.getText()+Mytest4_6.laboper2.getText()+Mytest4_6.jtuser1.getText()+Mytest4_6.laboper3.getText()+Mytest4_6.jtuser2.getText()+"*******正确结果*******"+Mytest4_6.riganw1.getText()+"/"+Mytest4_6.riganw2.getText()+"\\\n"
+Mytest4_7.lab1.getText()+Mytest4_7.lab5.getText()+Mytest4_7.lab2.getText()+Mytest4_7.laboper1.getText()+Mytest4_7.lab3.getText()+Mytest4_7.lab6.getText()+Mytest4_7.lab4.getText()+Mytest4_7.laboper2.getText()+Mytest4_7.jtuser1.getText()+Mytest4_7.laboper3.getText()+Mytest4_7.jtuser2.getText()+"******正确结果********"+Mytest4_7.riganw1.getText()+"/"+Mytest4_7.riganw2.getText()+"\\\n"
+Mytest4_8.lab1.getText()+Mytest4_8.lab5.getText()+Mytest4_8.lab2.getText()+Mytest4_8.laboper1.getText()+Mytest4_8.lab3.getText()+Mytest4_8.lab6.getText()+Mytest4_8.lab4.getText()+Mytest4_8.laboper2.getText()+Mytest4_8.jtuser1.getText()+Mytest4_8.laboper3.getText()+Mytest4_8.jtuser2.getText()+"******正确结果******"+Mytest4_8.riganw1.getText()+"和"+Mytest4_8.riganw2.getText()+"\\\n"
+Mytest5.lab1.getText()+Mytest5.laboper1.getText()+Mytest5.lab2.getText()+Mytest5.laboper2.getText()+Mytest5.lab3.getText()+Mytest5.lab4.getText()+Mytest5.jtuser1.getText()+","+Mytest5.lab5.getText()+Mytest5.jtuser2.getText()+"******正确结果******"+Mytest5.riganw1.getText()+"
和 "+Mytest5.riganw2.getText()+"\\\n"
+lab1.getText()+laboper1.getText()+lab2.getText()+lab4.getText()+jtuser1.getText()+","+lab5.getText()+jtuser2.getText()+"********正确答案********"+riganw1.getText()+" **和** "+riganw2.getText()+"\\\n"
+"你的总分是:"+Mytest5_2.score);
}
}
}
相关文章推荐
- Java实训——创建一个窗体程序,实现简单的数学加、减法测试。
- JAVA 周练一 (简单数学)
- 简单的java爬虫抓取网页实现代码(未测试)
- centos 安装redis java简单测试
- Java-idea-创建maven项目,部署项目,部署服务器,简单测试
- 用Java Swing编写简单的测试小工具界面(源码) 分类: Java 2015-07-30 10:49 23人阅读 评论(0) 收藏
- java例程练习(一个BirthDate类 以及简单的测试程序)
- [程序代写推荐]java代理课程测试 spring AOP代理简单测试
- MQTT——java简单测试
- 一个简单java小学数学循环输出,两次显示文框
- 一个Java实现的简单的多个客户端聊天程序(未测试)
- 用Java Swing编写简单的测试小工具界面(源码)
- Java简单数学运算工具类
- [找程序员代写推荐]java代理课程测试 spring AOP代理简单测试
- 一、问题 MyEclipse怎么导出可运行的jar包。 二、测试环境 MyEclipse V8.6 三、操作过程 1、java项目没有导入第三方jar包的情况 这时候打包就比较简单: ①首先在MyE
- MQTT——java简单测试
- Java Web开发环境塔建 及简单测试Java Web程序
- HDU 1840 Equations (简单数学 + 水题)(Java版)
- Java简单工具:UDP通讯工具类(测试使用)
- Java 7新特性及简单性能对比测试