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

结对编程《四则运算》

时间:2016-04-11 01:38:37      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

1.结对编程题目是:面向小学生,随机生成30个100以内的四则运算,并且用户可以录入结果,检查用户录入的结果是否是正确的。

2.结对编程对象:晏蜀杰,博客地址:http://www.cnblogs.com/ysjwj320/

3双方贡献度: 1:2

4.结对编程照片:

技术分享

5.源程序代码:

MyExpression.java

 

package com.yanshujie.calculator;

import java.util.Map;

public interface MyExpression {
	
	public void setCount(int count);
	
	public Map<String,Number> getExpressions();
}

  

ObtainExpression.java

 

package com.yanshujie.calculator;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.eclipse.jdt.annotation.NonNull;

public class ObtainExpression implements MyExpression{
	
	private int count = 100;
	private Number start;
	private Number end;
	private boolean isDecimal = true;
	private static final List<String> operators = Arrays.asList(new String[]{"+","-","*","/"});
	
	public ObtainExpression(@NonNull Number start, @NonNull Number end) throws IllegalArgumentException{
		setNumbers(start,end);
	}
	
	public ObtainExpression(@NonNull Number start, @NonNull Number end, int count) throws IllegalArgumentException{
		this(start,end);
		setCount(count);
	}
	
	@Override
	public void setCount(int count){
		if( !isDecimal ){
			int range = end.intValue() - start.intValue() + 1;
			if( this.count > range*(range-1)){
				throw new IllegalArgumentException("the count of expressions is bigger than the count of all expressions.");
			}
		}
		this.count = count;
	}
	
	@Override
	public Map<String,@NonNull Number> getExpressions(){
		Map<String,Number> map = new HashMap<String,Number>();
		if( !isDecimal ){
			int start = this.start.intValue();
			int end = this.end.intValue();
			Random random = new Random(System.currentTimeMillis());
			while(map.size() < this.count ){
				int leftNumber = random.nextInt(end-start+1)+start;
				int rightNumber = random.nextInt(end-start+1)+start;
				int oper = random.nextInt(operators.size());
				if( rightNumber == 0 && operators.get(oper).equals("/") ){
					continue;
				}
				StringBuilder builder = new StringBuilder();
				builder.append(leftNumber).append(operators.get(oper)).append(rightNumber).append("=");
				if( map.containsKey(builder.toString()) ){
					continue;
				}
				Number result = compute(leftNumber,rightNumber,operators.get(oper));
				map.put(builder.toString(), result);		
			}
		}else{
			// TODO to handle double numbers
		}
		
		return map;
	}
	
	private Number compute(int leftNumber, int rightNumber, String operator){
		Number result = null;
		switch(operator){
		case "+":
			result = leftNumber + rightNumber;
			break;
		case "-":
			result = leftNumber - rightNumber;
			break;
		case "*":
			result = leftNumber * rightNumber;
			break;
		case "/":
			result = (leftNumber*1.0) / rightNumber;
			break;
		default:
			break;
		}
		return result;
	}
	
	private void setNumbers(@NonNull Number start, @NonNull Number end){
		if( start instanceof Integer && end instanceof Integer){
			this.isDecimal = false;
		}
		if( start.doubleValue() > end.doubleValue() ){
			throw new IllegalArgumentException("the start number must be bigger than the end number");
		}
		this.start = start;
		this.end = end;
	}
}

  

Calculator.java

 

package com.yanshujie.calculator;

import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class Calculator {
	private static final Scanner input = new Scanner(System.in);
	
	public static void main(String[] args) {
		System.out.print("please input the start number:");
		int start = getInt();
		System.out.print("please input the end number:");
		int end = getInt();
		System.out.print("please input the count of expressions:");
		int count = getUnsignedInt();
		MyExpression myexp = new ObtainExpression(start, end);
		myexp.setCount(count);
		Map<String,Number> result = myexp.getExpressions();
		Set<String> keys = result.keySet();
		System.out.print("to output the result,yes(y) or no(n)?:");
		String chosen = getChosen();
		System.out.println("the expressions are: ");
		for( String key : keys ){
			if( chosen.equals("y") || chosen.matches("Y")){
				System.out.println(key + result.get(key));
			}else{
				System.out.println(key);
			}
		}
		realse();
	}
	
	public static String getChosen(){
		String chosen = null;
		if( input.hasNext() ){
			chosen = input.next();
		}
		while(!chosen.matches("^[yYnN]$")){
			System.out.print("please input ‘y‘ or ‘n‘:");
			if( input.hasNext()){
				chosen = input.next();
			}
		}
		return chosen;
	}
	
	public static int getUnsignedInt(){
		int unsignedNum = getInt();
		while(unsignedNum <= 0){
			System.out.print("input error,please input an unsigned number:");
			unsignedNum = getInt();
		}
		return unsignedNum;
	}
	
	public static int getInt(){
		do{
			if( input.hasNextInt() ){
				return input.nextInt();
			}else{
				System.out.print("input error,please input the number:");
			}
		}while(true);
	}
	
	public static void realse(){
		if( input != null ){
			input.close();
		}
	}
}

  

结对编程《四则运算》

标签:

原文地址:http://www.cnblogs.com/lovelyYakir/p/5376577.html

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