您的位置:首页 > 编程语言

结对编程实现小学四则运算

2016-03-16 15:45 309 查看

结对编程实现四则运算



前言:按照结对编程的原则,我和小组另一个成员进行了两人进行了结对编程,我们开发的结对项目的是小学的四则运算,具体就是随机生成30道题,供学生答题并返回正误。经过讨论,我们将采用Java的图形化界面完成,分工为两部分,一人负责四则运算的产生,一个界面以及运算后的界面。

功能:实现用户输入的数量,随机产生四则运算,用户输入答案,判断用户输入的正误并记录如一张表供用户查询。

下面是具体的代码及演示:

四则运算式的产生:(向外提供四则运算式String类借口、提供正确的答案以及各个运算符与运算式)Out.java

package com.cxy;

import java.util.Random;

public class Out {
private int a, b;
private int i;
private String operator[]={"+","-","×","÷"};

public String getYun(){
while(true){
a=new Random().nextInt(100);
b=new Random().nextInt(100);  //产生随机数
i=new Random().nextInt(4);

if(i==1&&a<b){         //不能为负数
continue;
}

if(i==3){             //不能被0除
if(b==0){
continue;
}
if(a%b!=0){      //a一定被b除
continue;
}
}
break;
}
return new String(a+operator[i]+b+"=");
}

public  boolean panduan(String s){    //判断答案是否正确
int i,result = 0;
try{
i=Integer.valueOf(s).intValue();
}catch(Exception e){
return false;
}
switch(this.operator()){
case "+":result=this.getA()+this.getB();break;
case "-":result=this.getA()-this.getB();break;
case "×":result=this.getA()*this.getB();break;
case "÷":result=this.getA()/this.getB();break;
}
if(result==i){
return true;
}return false;

}

public int correctResult(){     //返回正确的答案
int result=0;
switch(this.operator()){
case "+":result=this.getA()+this.getB();break;
case "-":result=this.getA()-this.getB();break;
case "×":result=this.getA()*this.getB();break;
case "÷":result=this.getA()/this.getB();break;
}
return result;
}

public String operator(){        //返回运算符
return operator[this.i];
}

public int getA() {  //返回运算数
return a;
}

public int getB() { //返回运算数
return b;
}

}


下面是界面以及各个控件的布局与事件处理

具体实现:用户先输入需要的题数,然后显示第一个四则运算,用户输入结果,软件判断正误并记录,显示用户剩余的题数以及目前正确率,然后显示第二个四则运算循环一直达到用户需要的题数。

代码如下:

package com.cxy.jg;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;   //引入相关的包

public class Ui {

public static JScrollPane jls;
public static int correct=0;
public static int number=0;
public static int total=0;
public static boolean flag=false;
public static JTextField count;
public static JButton btn_ok;
public static JButton btn_xuan;
public static JLabel operate;
public static JTextField result;
public static JLabel info;
public static JList infoList;         //声明各种控件
public static DefaultListModel dlm = new DefaultListModel();
public static Out out=new Out();

public static boolean panduan(String s){   //判断用户输入的题数是否是整数且在1-100的范围内
int i;
try
{
i=Integer.valueOf(s).intValue();
}catch(Exception e){
return false;
}
if(i<=0||i>100)
{
return false;
}
return true;
}

public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame=new JFrame("四则运算");
Container contentPane=frame.getContentPane();

contentPane.setLayout(new GridLayout(2,1));  //界面布局

JPanel panel_top=new JPanel();
panel_top.setLayout(new BorderLayout());

JPanel panel_one=new JPanel(new FlowLayout(FlowLayout.LEFT));
count=new JTextField(5);
btn_xuan=new JButton("确定");
btn_xuan.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

if(Ui.panduan(Ui.count.getText())){
Ui.out=new Out();
Ui.correct=0;
Ui.total=Ui.number=Integer.valueOf(Ui.count.getText()).intValue();
Ui.flag=true;
Ui.btn_ok.setEnabled(true);
Ui.result.setEnabled(true);
Ui.btn_xuan.setText("重新生成");

Ui.operate.setText(Ui.out.getYun());
Ui.info.setText("你还剩余"+Ui.number+"道题,你已经答对"+Ui.correct+"道题"+",你目前的正确率为"+new java.text.DecimalFormat("00.0%").format(((Ui.correct*1.0)/Ui.total)));
Ui.dlm.clear();
Ui.infoList.setModel(Ui.dlm);
}
}

});   //确定题数按钮事件的处理
panel_one.add(new JLabel("产生题目数:"));
panel_one.add(count);
panel_one.add(btn_xuan);
panel_one.add(new JLabel("1-100"));

JPanel panel_second=new JPanel(new FlowLayout(FlowLayout.LEFT));
operate=new JLabel("0+0=");
result=new JTextField(5);
result.setEnabled(false);
btn_ok=new JButton("提交");
btn_ok.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
StringBuffer s=new StringBuffer();
if(Ui.out.panduan(Ui.result.getText())){
Ui.correct++;
Ui.number--;

s.append(Ui.out.getA()).append(Ui.out.operator()).append(Ui.out.getB()+"=").append(Ui.result.getText()).append("\t").append("   √    ");

}else{
Ui.number--;
s.append(Ui.out.getA()).append(Ui.out.operator()).append(Ui.out.getB()+"=").append(Ui.result.getText()).append("\t").append("   ×    ").append(Ui.out.correctResult());

}
Ui.info.setText("你还剩余"+Ui.number+"道题,你已经答对"+Ui.correct+"道题"+",你目前的正确率为"+new java.text.DecimalFormat("00.0%").format(((Ui.correct*1.0)/Ui.total)));
if(Ui.number==0){
Ui.btn_ok.setEnabled(false);
Ui.info.setForeground(Color.red);
Ui.info.setText("恭喜你!你总共完成"+Ui.total+"道题"+",你的答题正确率为"+new java.text.DecimalFormat("00.0%").format(((Ui.correct*1.0)/Ui.total)));

}

Ui.dlm.addElement(s);
Ui.infoList.setModel(Ui.dlm);

Ui.out=new Out();
Ui.operate.setText(Ui.out.getYun());

}

}); //提交结果按钮事件处理
Ui.operate.setForeground(Color.BLUE);

btn_ok.setEnabled(false);
panel_second.add(new JLabel("         运算:"));
panel_second.add(operate);
panel_second.add(result);
panel_second.add(btn_ok);

JPanel panel_third=new JPanel(new FlowLayout(FlowLayout.LEFT));
info=new JLabel("");
panel_third.add(new JLabel("提示:"));
panel_third.add(info);

infoList=new JList();
jls=new JScrollPane(infoList);

panel_top.add(panel_one,BorderLayout.NORTH);
panel_top.add(panel_second,BorderLayout.CENTER);
panel_top.add(panel_third,BorderLayout.SOUTH);

contentPane.add(panel_top);
contentPane.add(jls);
frame.setSize(450, 500);
frame.setVisible(true);
}

}


运行展示









收获:

这次与另外同学的结对编程,发现自己某些方面的不足,某些考虑不全面,有时候同学都可以一针见血的指出,同时两个人同时编程感觉更高效考虑的更全面,如果是很多人同时开发,几个人之间的沟通就成很大问题,不一定就有结对编程的效率高。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: