标签:
package runok;
import java.util.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class vv extends JFrame implements ActionListener{
/**
* 作者:范铭祥
* 功能:一个简单的小学生四则运算 自动 生成系统
*/
JPanel jp1,jp2,jp3;
JLabel jlb1,jlb2;
JButton jb1;
JTextField jtf1,jtf2;
JTextArea ee;
public double w1;
public double m1;
String fh;
public static void main(String[] args)
{
vv start=new vv();
}
public vv()
{
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
jlb1=new JLabel("题目");
jlb2=new JLabel("你的答案:");
jb1=new JButton("判断答案");
jb1.addActionListener(this);
jb1.setActionCommand("aa");
JTextArea txtArea = new JTextArea(1,15);
jtf2=new JTextField(20);
ee = new JTextArea(1,10);
//设置布局管理
this.setLayout(new GridLayout(3,1));
//加入各个组件
jp1.add(jlb1);
jp1.add(txtArea);
jp2.add(jlb2);
jp2.add(jtf2);
jp3.add(jb1);
jp3.add(ee);
//加入到JFrame
this.add(jp1);
this.add(jp2);
this.add(jp3);
//在这里用yy方法来获取题目 w 符号 m
yy tt=new yy();
w1=tt.ret1();
m1=tt.ret2();
fh=tt.ret3();
txtArea.append(w1+fh+m1);
this.setSize(400, 250);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void yes()
{ ee.append("正确!"); }
public void no()
{ee.append("错误!");}
public void cc()
{
int answer=(int) (Double.parseDouble(jtf2.getText()));;
if(fh.equals("+"))
{
if(answer==w1+m1){
yes();
}else{
no();
}
}
if(fh.equals("-"))
{
if(answer==w1-m1){
yes();
}else{
no();
}
}
if(fh.equals("*"))
{
if(answer==w1*m1){
yes();
}else{
no();
}
}
if(fh.equals("/"))
{
if(answer==w1/m1){
yes();
}else{
no();
}
}
}
@Override
public void actionPerformed(ActionEvent e)
{
// !!!!
if(e.getActionCommand()=="aa")
{
cc();
}
}
}
class yy
{
private int max=4;
private int min=1;
private double w2,m2;
private String ff;
Random random = new Random();
//System.out.println(s);
public yy()
{
int s = random.nextInt(max)%(max-min+1) + min;
double x=random.nextInt(max-2)%(max-2-min+1) + min;
double y=random.nextInt(max-1)%(max-1-min+1) + min;
int y1up=(int) Math.pow(10,x);
int y1dn=(int) Math.pow(10,x-1);
int y2up=(int) Math.pow(10,y);
int y2dn=(int) Math.pow(10,y-1);
int x_y1=random.nextInt(y1up)%(y1up-y1dn+1) + y1dn;
int x_y2=random.nextInt(y2up)%(y2up-y2dn+1) + y2dn;
double w=(double)x_y1;
double m=(double)x_y2;
this.w2=w;
this.m2=m;
switch(s)
{
case 1:
{
this.ff="+";
System.out.println(w+"+"+m+"=" );
break;
}
case 2:
{
this.ff="-";
System.out.println(w+"-"+m+"=" );
break;
}
case 3:
{
this.ff="*";
System.out.println(w+"*"+m+"=" );
break;
}
case 4:
{
this.ff="/";
System.out.println(w+"/"+m+"=" );
break;
}
}
}
public double ret1()
{
return w2;
}
public double ret2()
{
return m2;
}
public String ret3()
{
return ff;
}
}
其中在我的小学生系统中这就是我的TDD
class yy
{
private int max=4;
private int min=1;
private double w2,m2;
private String ff;
Random random = new Random();
//System.out.println(s);
public yy()
{
int s = random.nextInt(max)%(max-min+1) + min;
double x=random.nextInt(max-2)%(max-2-min+1) + min;
double y=random.nextInt(max-1)%(max-1-min+1) + min;
int y1up=(int) Math.pow(10,x);
int y1dn=(int) Math.pow(10,x-1);
int y2up=(int) Math.pow(10,y);
int y2dn=(int) Math.pow(10,y-1);
int x_y1=random.nextInt(y1up)%(y1up-y1dn+1) + y1dn;
int x_y2=random.nextInt(y2up)%(y2up-y2dn+1) + y2dn;
double w=(double)x_y1;
double m=(double)x_y2;
this.w2=w;
this.m2=m;
switch(s)
{
case 1:
{
this.ff="+";
System.out.println(w+"+"+m+"=" );
break;
}
case 2:
{
this.ff="-";
System.out.println(w+"-"+m+"=" );
break;
}
case 3:
{
this.ff="*";
System.out.println(w+"*"+m+"=" );
break;
}
case 4:
{
this.ff="/";
System.out.println(w+"/"+m+"=" );
break;
}
}
}
public double ret1()
{
return w2;
}
public double ret2()
{
return m2;
}
public String ret3()
{
return ff;
}
}
只要在主程序中运用到这个类就可以得到 两个数和一个算术运算符
队友 黄国柱 http://www.cnblogs.com/zzhuzi/
标签:
原文地址:http://www.cnblogs.com/vip-fan1234xiang/p/4488467.html