码迷,mamicode.com
首页 > 其他好文 > 详细

结对编程——四则运算

时间:2016-04-08 19:43:33      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

学习进度条:

点滴成就 学习时间 新编写代码行数 博客量(篇) 学习知识点
第一周 10小时  0  0 了解软件工程
第二周 10小时  0  1  项目开题
第三周 15小时  0  1  开通博客、开展项目调查
第四周 20小时  200 1  需求文档、用例图、代码规范

第五周 15小时 0 0 软件工程
第六周 20小时   218 0 编译原理、软件工程详细设计
第七周 18小时 81 1 软件工程

1. 结对编程对象:胡磊 2013110410

  对方博客地址:http://home.cnblogs.com/u/leihu/

   双方贡献比例: 1:1

技术分享

2. 源代码

技术分享
package com.fancy.exam;

import java.util.Random;

public class Test {
    //一道四则运算题
    private  int number1;
    private  int number2;
    private String sig;
    private int result;
    private  String[] signal = {"+","-","*","/"}; 
    
    public Test(){
        Random rand = new Random();
        int temp = rand.nextInt(4);
        this.sig = signal[temp];
        this.number1 = rand.nextInt(100);
        this.number2 = rand.nextInt(100);
        if(this.sig.equals("/")){
            if(this.number2 != 0){        //合法的除法运算
                while(this.number1%this.number2!=0)
                {
                    this.number1 = rand.nextInt(100);
                }
            }else{        //除数为零
                this.number2 = rand.nextInt(100);
            }
        }
        switch(this.sig){
        case "+":
            this.result = this.number1 + this.number2;
            break;
        case "-":
            this.result = this.number1 - this.number2;
            break;
        case "*":
            this.result = this.number1 * this.number2;
            break;
        case "/":
            this.result = this.number1 / this.number2;
            break;
        }
        
    }
    
    
    public int getNumber1() {
        return number1;
    }


    public void setNumber1(int number1) {
        this.number1 = number1;
    }


    public int getNumber2() {
        return number2;
    }


    public void setNumber2(int number2) {
        this.number2 = number2;
    }


    public String getSig() {
        return sig;
    }


    public void setSig(String sig) {
        this.sig = sig;
    }


    public int getResult() {
        return result;
    }


    public void setResult(int result) {
        this.result = result;
    }


    public void show(){
        System.out.print(this.number1 + " " + this.number2 + " = " );
    }
}
一道四则运算的类
技术分享
package com.fancy.exam;

public class Tests {
    //习题集类
    private Test tests[];
    private int count;        //习题数目
    
    public Tests(int count){
        if(count>0){
            this.count = count;
        }
        tests = new Test[count];
        for(int i = 0;i<count;i++)
            tests[i] = new Test();
    }
    
    public void printTests(){    //打印习题
        for(int i=0;i<count;i++){
            System.out.print(tests[i].getNumber1() + " " + tests[i].getSig() + " " + tests[i].getNumber2() + " =\t   ");
            if((i+1)%5==0)
                System.out.println();
        }
    }
    
    public void printAnswers(){   //打印答案
        System.out.println("答案如下");
        for(int i=0;i<count;i++){
            System.out.print(tests[i].getNumber1() + " " + tests[i].getSig() + " " + tests[i].getNumber2() + " = " + tests[i].getResult() + "       \t");
            if((i+1)%5==0)
                System.out.println();
        }
    }
}
四则运算习题集类
技术分享
package com.fancy.exam;

import java.util.Random;
import java.util.Scanner;

public class Tester {
     //测试类
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        Scanner input = new Scanner(System.in);
        int count;
        System.out.print("请输入题目数量:");
        count = input.nextInt();
        Tests tests = new Tests(count);
        tests.printTests();
        System.out.println("是否打印答案?是:Y/y 不,直接退出:N/n");
        String choice = input.next();
        if(choice.equals("Y") || choice.equals("y")){
            tests.printAnswers();
        }else{
            System.out.println("谢谢使用,再见。");
        }
        input.close();
    }

}
测试类

 

3. 输入和输出

技术分享

结对编程——四则运算

标签:

原文地址:http://www.cnblogs.com/FancyLian/p/5369691.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!