一、收获:
通过这次作业学会了在eclipse中适用junit4进行单元测试。
我的主要工作是领航和测试:
封装和打包之后便发现了原来程序中的很多问题
二、错误检验:
1.一开始没注意到运算符这里的输入数不能下于0,后来加入一个if判断:
if(fhnum<=0){JOptionPane.showMessageDialog(null, "符号数不能小于0,请重新输入!");}
2.原来的程序在正确答案的运算是没出现浮点型的,通过对出现除法部份的强制转换,使其结果出现了小数,又不丢失前面的位数。
解决方法:
比如原来的ranswer=a/b;
改成ranswer=a*1.0/b;
import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import java.util.Scanner; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import java.awt.Color; public class yun { public static void main(String[] args) { new Calculator(); } } class Calculator extends JFrame{ private JLabel L1,L2,L3,L4; //private JLabel L5; private JTextField T1,T2,T3,T4,T5; private JButton B1,B2,B3;//按钮 int a,b,c,d,h; char fh2,fh3,fh4; char[]fh={'+','-','*','/'}; Scanner in=new Scanner(System.in); double ranswer; int answer;//正确答案和用户输入答案 int num,fhnum;//出题数和符号数 int wrong=0,right=0;//对错题数 int score=0; int sum=0; public Calculator(){ this("四则运算"); } public Calculator(String title){ super(title);//设置窗体标题 setSize(510,150);//设置窗体大小 //屏幕居中显示 Dimension size=Toolkit.getDefaultToolkit().getScreenSize();//获得屏幕的标尺 int screenWidth=size.width; int screenHeight=size.height; int x=(screenWidth-this.getWidth())/2; int y=(screenHeight-this.getHeight())/2; this.setLocation(x, y); //设置关闭按钮操作 this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setResizable(false);//设置窗体大小不可改变 this.setVisible(true);//设置窗体可见 //创建界面元素 L1=new JLabel("选择所需的题目数(1-5):"); T1=new JTextField(10); //放题目个数 L2=new JLabel("运算符的个数:"); T2=new JTextField(10); //放运算符个数 B1=new JButton("开始"); L3=new JLabel("四则运算式:"); T3=new JTextField(10); //设置运算式 L4=new JLabel("输入答案:"); T4=new JTextField(10); //获取用户输入答案 B2=new JButton("下一题"); T5=new JTextField(10);//获取正确答案 B3=new JButton("确定"); //L5=new JLabel("计时"); //创建面板 JPanel p2=new JPanel(); JPanel p1=new JPanel(); p2.add(L1); p2.add(T1); p2.add(L2); p2.add(T2); p2.add(L3); p2.add(T3); p2.add(L4); p2.add(T4); // p2.add(L5); p1.add(B1); p1.add(B2); p1.add(B3); add(p2,"Center"); add(p1,"South"); //设置窗体界面颜色 p1.setBackground(Color.gray); p2.setBackground(Color.gray); B1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { num=Integer.parseInt(T1.getText()); fhnum=Integer.parseInt(T2.getText()); sum++; a=new Random().nextInt(100); b=new Random().nextInt(100); c=new Random().nextInt(100); d=new Random().nextInt(100); h=new Random().nextInt(4); fh2=fh[h]; fh3=fh[h]; fh4=fh[h]; if(fhnum<=0){JOptionPane.showMessageDialog(null, "符号数不能小于0,请重新输入!");} if(num>5) { JOptionPane.showMessageDialog(null, "输入有误,请重新输入!"); } if(num<=5){ if(fhnum==1)//一个运算符 { T3.setText(a+String.valueOf(fh2)+b+"="); if(fh2=='+'){ ranswer=a+b; } if(fh2=='-'){ ranswer=a-b; } if(fh2=='*'){ ranswer=a*b; } if(fh2=='/'){ ranswer=a*1.0/b; } } if(fhnum==2)//两个运算符 { T3.setText(a+String.valueOf(fh2)+b+String.valueOf(fh3)+c+"="); if(fh2=='+') { if(fh3=='+'){ ranswer=a+b+c; } if(fh3=='-'){ ranswer=a+b-c; } if(fh3=='*'){ ranswer=a+b*c; } if(fh3=='/'){ ranswer=a+b*1.0/c; } } if(fh2=='-') { if(fh3=='+'){ ranswer=a-b+c; } if(fh3=='-'){ ranswer=a-b-c; } if(fh3=='*'){ ranswer=a-b*c; } if(fh3=='/'){ ranswer=a-b*1.0/c; } } if(fh2=='*') { if(fh3=='+'){ ranswer=a*b+c; } if(fh3=='-'){ ranswer=a*b-c; } if(fh3=='*'){ ranswer=a*b*c; } if(fh3=='/'){ ranswer=a*b*1.0/c; } } if(fh2=='/') { if(fh3=='+'){ ranswer=a*1.0/b+c; } if(fh3=='-'){ ranswer=a*1.0/b-c; } if(fh3=='*'){ ranswer=a*1.0/b*c; } if(fh3=='/'){ ranswer=a*1.0/b/c; } } } if(fhnum==3)//三个运算符 { T3.setText(a+String.valueOf(fh2)+b+String.valueOf(fh3)+c+String.valueOf(fh3)+d+"="); if(fh2=='+')//第一为加 { if(fh3=='+') { if(fh4=='+') {ranswer=a+b+c+d;} if(fh4=='-') {ranswer=a+b+c-d;} if(fh4=='*') {ranswer=a+b+c*d;} if(fh4=='/') {ranswer=a+b+c*1.0/d;} } if(fh3=='-') { if(fh4=='+') {ranswer=a+b-c+d;} if(fh4=='-') {ranswer=a+b-c-d;} if(fh4=='*') {ranswer=a+b-c*d;} if(fh4=='/') {ranswer=a+b-c*1.0/d;} } if(fh3=='*') { if(fh4=='+') {ranswer=a+b*c+d;} if(fh4=='-') {ranswer=a+b*c-d;} if(fh4=='*') {ranswer=a+b*c*d;} if(fh4=='/') {ranswer=a+b*c*1.0/d;} } if(fh3=='/') { if(fh4=='+') {ranswer=a+b*1.0/c+d;} if(fh4=='-') {ranswer=a+b*1.0/c-d;} if(fh4=='*') {ranswer=a+b*1.0/c*d;} if(fh4=='/') {ranswer=a+b*1.0/c/d;} } } if(fh2=='-')//第一为减 { if(fh3=='+') { if(fh4=='+') {ranswer=a-b+c+d;} if(fh4=='-') {ranswer=a-b+c-d;} if(fh4=='*') {ranswer=a-b+c*d;} if(fh4=='/') {ranswer=a-b+c*1.0/d;} } if(fh3=='-') { if(fh4=='+') {ranswer=a-b-c+d;} if(fh4=='-') {ranswer=a-b-c-d;} if(fh4=='*') {ranswer=a-b-c*d;} if(fh4=='/') {ranswer=a-b-c*1.0/d;} } if(fh3=='*') { if(fh4=='+') {ranswer=a-b*c+d;} if(fh4=='-') {ranswer=a-b*c-d;} if(fh4=='*') {ranswer=a-b*c*d;} if(fh4=='/') {ranswer=a-b*c*1.0/d;} } if(fh3=='/') { if(fh4=='+') {ranswer=a-b*1.0/c+d;} if(fh4=='-') {ranswer=a-b*1.0/c-d;} if(fh4=='*') {ranswer=a-b*1.0/c*d;} if(fh4=='/') {ranswer=a-b*1.0/c/d;} } } if(fh2=='*')//第一为乘 { if(fh3=='+') { if(fh4=='+') {ranswer=a*b+c+d;} if(fh4=='-') {ranswer=a*b+c-d;} if(fh4=='*') {ranswer=a*b+c*d;} if(fh4=='/') {ranswer=a*b+c*1.0/d;} } if(fh3=='-') { if(fh4=='+') {ranswer=a*b-c+d;} if(fh4=='-') {ranswer=a*b-c-d;} if(fh4=='*') {ranswer=a*b-c*d;} if(fh4=='/') {ranswer=a*b-c*1.0/d;} } if(fh3=='*') { if(fh4=='+') {ranswer=a*b*c+d;} if(fh4=='-') {ranswer=a*b*c-d;} if(fh4=='*') {ranswer=a*b*c*d;} if(fh4=='/') {ranswer=a*b*c*1.0/d;} } if(fh3=='/') { if(fh4=='+') {ranswer=a*b*1.0/c+d;} if(fh4=='-') {ranswer=a*b*1.0/c-d;} if(fh4=='*') {ranswer=a*b*1.0/c*d;} if(fh4=='/') {ranswer=a*b*1.0/c/d;} } if(fh2=='/')//第一为除 { if(fh3=='+') { if(fh4=='+') {ranswer=a*1.0/b+c+d;} if(fh4=='-') {ranswer=a*1.0/b+c-d;} if(fh4=='*') {ranswer=a*1.0/b+c*d;} if(fh4=='/') {ranswer=a*1.0/b+c/d;} } if(fh3=='-') { if(fh4=='+') {ranswer=a*1.0/b-c+d;} if(fh4=='-') {ranswer=a*1.0/b-c-d;} if(fh4=='*') {ranswer=a*1.0/b-c*d;} if(fh4=='/') {ranswer=a*1.0/b-c/d;} } if(fh3=='*') { if(fh4=='+') {ranswer=a*1.0/b*c+d;} if(fh4=='-') {ranswer=a*1.0/b*c-d;} if(fh4=='*') {ranswer=a*1.0/b*c*d;} if(fh4=='/') {ranswer=a*1.0/b*c/d;} } if(fh3=='/') { if(fh4=='+') {ranswer=a*1.0/b/c+d;} if(fh4=='-') {ranswer=a*1.0/b/c-d;} if(fh4=='*') {ranswer=a*1.0/b/c*d;} if(fh4=='/') {ranswer=a*1.0/b/c/d;} } } } }}} }); B3.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ if(ranswer==Integer.parseInt(T4.getText())){ JOptionPane.showMessageDialog(null, "恭喜你!答对了"); right++; } if(ranswer!=Integer.parseInt(T4.getText())){ JOptionPane.showMessageDialog(null,"很遗憾!答错了!正确答案是"+ranswer); wrong++; } } }); B2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { if(sum==num) { JOptionPane.showMessageDialog(null, "答题结束!答对了"+right+",答错了"+wrong); System.exit(0); } else { T3.setText(null); T4.setText(null); } } } ); } }
五、心得体会:
在这次做作业,也发现了上次作业遗漏的一些部分比如在三个运算符那里就漏了一种情况。
导致运算结果偶尔出错。
因为时间问题还有一些发现的问题还没解决,后续将继续补上。
合作伙伴:朱建颖 http://www.cnblogs.com/zjy666/