1. Title Description:
for elementary school students, randomly generate 30 or less 100 arithmetic, and users can enter the results, check whether the user input results are correct.
2. Pairing programming Instructions
Pairs of objects: Yu Jian Blog Address: http://www.cnblogs.com/lovelyYakir/
Contributions by both parties: 2:1
3. This procedure describes:
A. This procedure has been partially improved on the basis of the original topic:
1. Randomly generate a specified number of arithmetic.
2. You can enter the upper and lower limits of the arithmetic.
4. Pair programming Photos:
5. SOURCE program code:
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;p Rivate static final list<string> operators = arrays.aslist (new string[]{"+", "-", "*", "/"});p Ublic 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);} @Overridepublic 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 Expres Sions. ");}} This.count = Count;} @Overridepublic Map<string, @NonNull number> getexpressions () {map<string,number> Map = new hashmap<string,number> (); (!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 "+": r Esult = 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);p ublic 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 is:"); 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 ();}}}
Pairing Programming Summary: A simple arithmetic generation program