标签:
1.结对伙伴:叶煜稳 博客:http://www.cnblogs.com/doubi2wy/
四则运算核心计算代码:
if(e.getSource()==B1){
String str=T1.getText();
num=Integer.parseInt(str);
}
for(int i=0;i<num;i++){
int m;
m=(int)(Math.random()*3+1);
if(e.getSource()==B2)
if(m==3){
a=(int)(Math.random()*100+1);
b=(int)(Math.random()*100+1);
T2.setText(a+"+"+"("+b+")=?");
result=a+b;
if(e.getSource()==B3){
num1=Integer.parseInt(T3.getText());
if(num1==result){
T4.setText("回答正确!!");
sum=sum+10;
}
else
{
T4.setText("回答错误!!!"+result);
}
}
}
else if(m==1){
q=(int)(Math.random()*100+1);
w=(int)(Math.random()*100+1);
T2.setText(q+"*"+"("+w+")"+"=?");
if(e.getSource()==B3){
num1=Integer.parseInt(T3.getText());
result1=q*w;
if(num1==result1){
T4.setText("回答正确!!");
sum=sum+10;
}
else
{T4.setText("回答错误!!!"+result1);}
}
} else if(m==2){
j=(int)(Math.random()*100+1);
k=(int)(Math.random()*100+1);
if(k==0)
k++;
T2.setText(j+"/"+"("+k+")"+"=?");
if(e.getSource()==B3){
num2=Double.parseDouble(T3.getText());
result2=(double)(j/k)*1.00;
if(num2==result2){
T4.setText("回答正确!!");
sum=sum+10;
}
else
{T4.setText("回答错误!!!"+result2);}
}
}
}
}
}
将它封装成一个类:
import java.awt.event.ActionEvent;
import java.util.Random;
public class Unit {
public void calc(long result,double result1,double result2,int sum,int num,int num1,double num2,int a,int b,int q,int w,int j,int k){
Random r=new Random();
if(e.getSource()==B1){
String str=T1.getText();
num=Integer.parseInt(str);
}
for(int i=0;i<num;i++){
int m;
m=(int)(Math.random()*3+1);
if(e.getSource()==B2)
if(m==3){
a=(int)(Math.random()*100+1);
b=(int)(Math.random()*100+1);
T2.setText(a+"+"+"("+b+")=?");
result=a+b;
if(e.getSource()==B3){
num1=Integer.parseInt(T3.getText());
if(num1==result){
T4.setText("回答正确!!");
sum=sum+10;
}
else
{
T4.setText("回答错误!!!"+result);
}
}
}
else if(m==1){
q=(int)(Math.random()*100+1);
w=(int)(Math.random()*100+1);
T2.setText(q+"*"+"("+w+")"+"=?");
if(e.getSource()==B3){
num1=Integer.parseInt(T3.getText());
result1=q*w;
if(num1==result1){
T4.setText("回答正确!!");
sum=sum+10;
}
else
{T4.setText("回答错误!!!"+result1);}
}
} else {
j=(int)(Math.random()*100+1);
k=(int)(Math.random()*100+1);
if(k==0)
k++;
T2.setText(j+"/"+"("+k+")"+"=?");
if(e.getSource()==B3){
num2=Double.parseDouble(T3.getText());
result2=(double)(j/k)*1.00;
if(num2==result2){
T4.setText("回答正确!!");
sum=sum+10;
}
else
{T4.setText("回答错误!!!"+result2);}
}
}
}
}
}
}
Unit unit=new Unit();
unit.calc(result,result1,result2,sum,num,num1,num2,a,b,q,w,j,k);
但是不知为什么封装成的类会出错,有监听要怎么解决啊!
标签:
原文地址:http://www.cnblogs.com/lixin20/p/4468747.html