zoukankan      html  css  js  c++  java
  • 结对编程之四则运算

    学习进度条

    点滴成就 学习时间 新编写代码行数 博客量(篇) 学习知识点
    第一周 2h 0 0 了解软件工程
    第二周 3h 0 1 项目开题
    第三周 5h 0 1 需求文档、用例图
    第四周 8h 100 1 结对编程
    第五周 12h 120 0 软件工程
    第六周 15h 200 0 编译原理、软件工程详细设计
    第七周 20h 100 1 软件测试

    1. 结对编程对象:范心莲 2013110410

      对方博客地址:http://www.cnblogs.com/FancyLian/

       双方贡献比例: 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() + " =	   ");
                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() + "       	");
                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.输入输出

    
    
  • 相关阅读:
    73. Set Matrix Zeroes
    289. Game of Live
    212. Word Search II
    79. Word Search
    142. Linked List Cycle II
    141. Linked List Cycle
    287. Find the Duplicate Number
    260. Single Number III
    137. Single Number II
    Oracle EBS中有关Form的触发器的执行顺序
  • 原文地址:https://www.cnblogs.com/leihu/p/5375343.html
Copyright © 2011-2022 走看看