1. Program Design Ideas
1) generate two random numbers first;
2) Place the symbol into a 4-length array, generating random symbols by randomly generating 0-3 random numbers
3) again when the calculation, divided into two, one is a false fractional operation, the other is not fractional operations, which are generated by random;
4) Then use the switch to select the statement to carry out four different operations;
5) When running to division, consider that the denominator cannot be 0.
2 program code
Packagearithmetic;ImportJava.util.Random; Public classfouroperations { Public Static voidMain (String args[]) {string Firstnumber=NULL, Secondnumber =NULL; Random Random=NewRandom (); for(inti=0;i<20;i++) { intTemp=math.abs (Random.nextint ()%2); if(temp==1) {Firstnumber= "(" +string.valueof (Math.Abs (Random.nextint ()%100) + "/" +string.valueof (Math.Abs (Random.nextint ()%100) + ")"); Secondnumber= "(" +string.valueof (Math.Abs (Random.nextint ()%100) + "/" +string.valueof (Math.Abs (Random.nextint ()%100) + ")"); } Else { intf=Random.nextint (); Firstnumber=string.valueof (Math.Abs (f%100)); ints=Random.nextint (); Secondnumber=string.valueof (Math.Abs (s%100)); } Charfuhao[]={' + ', '-', ' * ', '/'}; intk=Random.nextint (); intDemp=math.abs (k%4); Switch(Fuhao[demp]) { Case+: {System.out.println (Firstnumber+ "+" +secondnumber+ "="); } Break; Case‘-‘: {System.out.println (Firstnumber+ "-" +secondnumber+ "="); } Break; Case‘*‘: {System.out.println (Firstnumber+ "*" +secondnumber+ "="); } Break; Case‘/‘: { if(secondnumber!= "0") {System.out.println (Firstnumber+ "/" +secondnumber+ "="); } } } } }}
3 Running Results
(87/48)-(2/79) =
(43/68) + (48/25) =
96*74=
97*56=
(73/9)/(10/98) =
(54/5)-(29/41) =
(22/51) + (13/70) =
(2/53) * (8/75) =
0-28=
(39/3)-(45/66) =
57/22=
(1/50) * (36/92) =
37*44=
(46/44) * (39/74) =
33+58=
38-27=
65-3=
16+63=
(71/32)/(40/73) =
20*41=
4. The reason why the individual class is not completed is because: I use the Java language, some Java language is not very familiar with, so need to find the code examples previously learned, resulting in the class is not completed on time.
After-school assignments 1