做業5.2 TDD

來源:互聯網
上載者:User

標籤:

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/

做業5.2 TDD

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.