-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomOps.java
74 lines (61 loc) · 1.36 KB
/
RandomOps.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.util.Random;
class RandomOps {
Random op = new Random();
public String[] next() {
int operId = op.nextInt(4) +1;
switch (operId) {
case 1:
//sum
return sum();
case 2 :
return subtrack();
case 3 :
return multiply();
case 4 :
return divide();
}
throw new UnknownError();
}
public String[] sum() {
int x = op.nextInt(99) + 1;
int y = op.nextInt(99) + 1;
String s = x + " + " + y +" = ";
Integer result = x + y;
return new String[]{s,result.toString()};
}
public String[] subtrack() {
int x = op.nextInt(99) + 1;
int y = op.nextInt(99) + 1;
String s = x + " - " + y +" = ";
Integer result = x - y;
return new String[]{s,result.toString()};
}
public String[] multiply() {
int x = op.nextInt(9) + 1;
int y = op.nextInt(9) + 1;
String s = x + " * " + y +" = ";
Integer result = x * y;
return new String[]{s,result.toString()};
}
public String[] divide() {
int x;
loopX:
do {
x = op.nextInt(99) + 1;
for(int i = 2;i<8;i++) {
if((x%i) == 0) {
break loopX;
}
}
}while(true);
int y;
loopY:
do {
y= op.nextInt(9) + 1;
if((x % y) ==0) break loopY;
}while(true);
String s = x + " / " + y +" = ";
Integer result = x / y;
return new String[]{s,result.toString()};
}
}