本文主要介绍了随机的四则运算题目,这样就可以自动的生成随机的30个四则运算题目了。可以生成随机的四则运算题目给给小学生用,但是还是有问题,小学生啊!他们不知道负数,不知道小数。所以还要加入判定条件。具体代码如下:
?
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 |
switch (Arr[n]){ case "+" :{ sum=a+b; break ; } case "-" :{ sum=a-b; while (sum<0){ var a = GetRandomNum(1,30); var b = GetRandomNum(1,30); sum=a-b; } break ; } case "*" :{ sum=a*b; break ; } case "/" :{ sum=a/b; while (hasDot(sum)){ var a = GetRandomNum(1,100); var b = GetRandomNum(1,100); sum=a/b; } break ; } |
这样负数和小数都没有了。
不过,小学生貌似会分数的运算。于是我又开始试试写分数的。
?
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 |
type= 'button' onclick= "pl_f()" >开始生成题目
function GetRandomNum(Min,Max) { var Range = Max - Min; var Rand = Math.random(); return (Min + Math.round(Rand * Range)); } function max_num(x,y) { var t; while (y) { t = x%y; x = y; y = t; } return x; } function fenshu(a,b,p) { if (p==1) { if (a>b) { return a+ "/" +b; } else if (a return a+ "/" +b; } else { return 1; } } else { var x=a/p; var y=b/p; if (y==1){ return x; } else { return x+ "/" +y; } } } function pl_f() { var son_a = GetRandomNum(1,30); var par_b = GetRandomNum(1,30); var son_c = GetRandomNum(1,30); var par_d = GetRandomNum(1,30); var X=son_a+ "/" +par_b; var Y=son_c+ "/" +par_d; document.write( "第一个分数:" +X+ " ); document.write( "第二个分数" +Y+ " ); max_x=max_num(son_a,par_b); max_y=max_num(son_c,par_d); document.write( "第一个最大公约数" +max_x+ " ); document.write( "第二个最大公约数" +max_y+ " ); var N=fenshu(son_a,par_b,max_x); var M=fenshu(son_c,par_d,max_y); document.write( "第一个分数化简:" +N+ " ); document.write( "第二个分数化简" +M); }
|
这个可以动态的生成两个分数,然后输出这个分数分子和分母的最大公约数。最后输出分子的最简式。
最后就是将分数放入整数一起了!
?
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167 |
content= "text/html; charset=utf-8" >
content= "editplus" />
content= "" />
content= "" />
content= "" />
onclick= "play_all()" >开始生成题目
function GetRandomNum(Min,Max) { var Range = Max - Min; var Rand = Math.random(); return (Min + Math.round(Rand * Range)); } function max_num(x,y) { var t; while (y) { t = x%y; x = y; y = t; } return x; } function fenshu(a,b,p) { if (p==1) { if (a>b) { return a+ "/" +b; } else if (a return a+ "/" +b; } else { return 1; } } else { var x=a/p; var y=b/p; if (y==1){ return x; } else { return x+ "/" +y; } } } function f_count(a,b,c,d) { var Arr = [ "+" , "-" , "*" , "/" ]; var n = GetRandomNum(0,3); switch (Arr[n]){ case "+" :{ var sum_x=(a*d)+(b*c); var sum_y=b*d; max_x=max_num(sum_x,sum_y); max_y=max_num(sum_x,sum_y); var N=fenshu(sum_x,sum_y,max_x); return a+ "/" +b+ "+" +c+ "/" +d+ "=" +N; break ; } case "-" :{ var sum_x=(a*d)-(b*c); while (sum_x<0) { var a = GetRandomNum(1,30); var b = GetRandomNum(1,30); var c = GetRandomNum(1,30); var d = GetRandomNum(1,30); var sum_x=(a*d)-(b*c); } var sum_y=b*d; max_x=max_num(sum_x,sum_y); max_y=max_num(sum_x,sum_y); var N=fenshu(sum_x,sum_y,max_x); return a+ "/" +b+ "-" +c+ "/" +d+ "=" +N; break ; } case "*" :{ var sum_x=a*c; var sum_y=b*d; max_x=max_num(sum_x,sum_y); max_y=max_num(sum_x,sum_y); var N=fenshu(sum_x,sum_y,max_x); return a+ "/" +b+ "*" +c+ "/" +d+ "=" +N; break ; } case "/" :{ var sum_x=a*d; var sum_y=b*c; max_x=max_num(sum_x,sum_y); max_y=max_num(sum_x,sum_y); var N=fenshu(sum_x,sum_y,max_x); return a+ "/" +b+ "÷" +c+ "/" +d+ "=" +N; break ; } } } function count(a,b){ var Arr = [ "+" , "-" , "*" , "/" ]; var n = GetRandomNum(0,3); switch (Arr[n]){ case "+" :{ var sum=a+b; return a+ "+" +b+ "=" +sum; break ; } case "-" :{ var sum=a-b; while (sum<0){ var a = GetRandomNum(1,30); var b = GetRandomNum(1,30); var sum=a-b; } return a+ "-" +b+ "=" +sum; break ; } case "*" :{ var sum=a*b; return a+ "*" +b+ "=" +sum; break ; } case "/" :{ var sum=a/b; while (a%b!==0){ var max_x=max_num(a,b); var N=fenshu(a,b,max_x); return a+ "÷" +b+ "=" +N; } return a+ "÷" +b+ "=" +sum; break ; } } } function pl_f() { var son_a = GetRandomNum(1,30); var par_b = GetRandomNum(1,30); var son_c = GetRandomNum(1,30); var par_d = GetRandomNum(1,30); var suma=f_count(son_a,par_b,son_c,par_d); return suma; } function pl() { var a = GetRandomNum(1,30); var b = GetRandomNum(1,30); var sumb=count(a,b); return sumb; } function play_all() { for (i = 1; i <=30; i++) { var a = GetRandomNum(0,1); document.getElementById( "text" ).innerHTML+=( " + "第" +i+ "题:" ); switch (a) { case 0: var A=pl(); document.getElementById( "text" ).innerHTML+=(A); break ; case 1: var B=pl_f(); document.getElementById( "text" ).innerHTML+=(B); break ; } } }
|
执行完节目就成这样的啦
最后上个答案和题目分开的版本!
?
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172 |
content= "text/html; charset=utf-8" >
content= "editplus" />
content= "" />
content= "" />
content= "" />
onclick= "play_all()" >开始生成题目
function GetRandomNum(Min,Max) { var Range = Max - Min; var Rand = Math.random(); return (Min + Math.round(Rand * Range)); } function max_num(x,y) { var t; while (y) { t = x%y; x = y; y = t; } return x; } function fenshu(a,b,p) { if (p==1) { if (a>b) { return a+ "/" +b; } else if (a return a+ "/" +b; } else { return 1; } } else { var x=a/p; var y=b/p; if (y==1){ return x; } else { return x+ "/" +y; } } } function f_count(a,b,c,d) { var Arr = [ "+" , "-" , "*" , "/" ]; var n = GetRandomNum(0,3); switch (Arr[n]){ case "+" :{ var sum_x=(a*d)+(b*c); var sum_y=b*d; max_x=max_num(sum_x,sum_y); max_y=max_num(sum_x,sum_y); var N=fenshu(sum_x,sum_y,max_x); return a+ "/" +b+ "+" +c+ "/" +d+ "=" +N; break ; } case "-" :{ var sum_x=(a*d)-(b*c); while (sum_x<0) { var a = GetRandomNum(1,30); var b = GetRandomNum(1,30); var c = GetRandomNum(1,30); var d = GetRandomNum(1,30); var sum_x=(a*d)-(b*c); } var sum_y=b*d; max_x=max_num(sum_x,sum_y); max_y=max_num(sum_x,sum_y); var N=fenshu(sum_x,sum_y,max_x); return a+ "/" +b+ "-" +c+ "/" +d+ "=" +N; break ; } case "*" :{ var sum_x=a*c; var sum_y=b*d; max_x=max_num(sum_x,sum_y); max_y=max_num(sum_x,sum_y); var N=fenshu(sum_x,sum_y,max_x); return a+ "/" +b+ "*" +c+ "/" +d+ "=" +N; break ; } case "/" :{ var sum_x=a*d; var sum_y=b*c; max_x=max_num(sum_x,sum_y); max_y=max_num(sum_x,sum_y); var N=fenshu(sum_x,sum_y,max_x); return a+ "/" +b+ "÷" +c+ "/" +d+ "=" +N; break ; } } } function count(a,b){ var Arr = [ "+" , "-" , "*" , "/" ]; var n = GetRandomNum(0,3); switch (Arr[n]){ case "+" :{ var sum=a+b; return a+ "+" +b+ "=" +sum; break ; } case "-" :{ var sum=a-b; while (sum<0){ var a = GetRandomNum(1,30); var b = GetRandomNum(1,30); var sum=a-b; } return a+ "-" +b+ "=" +sum; break ; } case "*" :{ var sum=a*b; return a+ "*" +b+ "=" +sum; break ; } case "/" :{ var sum=a/b; while (a%b!==0){ var max_x=max_num(a,b); var N=fenshu(a,b,max_x); return a+ "÷" +b+ "=" +N; } return a+ "÷" +b+ "=" +sum; break ; } } } function pl_f() { var son_a = GetRandomNum(1,30); var par_b = GetRandomNum(1,30); var son_c = GetRandomNum(1,30); var par_d = GetRandomNum(1,30); var suma=f_count(son_a,par_b,son_c,par_d); return suma; } function pl() { var a = GetRandomNum(1,30); var b = GetRandomNum(1,30); var sumb=count(a,b); return sumb; } function play_all() { for (i = 1; i <=30; i++) { var a = GetRandomNum(0,1); document.getElementById( "text" ).innerHTML+=( " + "第" +i+ "题:" ); switch (a) { case 0: var A=pl(); var strs = A.split( "=" ); document.getElementById( "text" ).innerHTML+=(strs[0]+ "=" + " ); document.getElementById( "text2" ).innerHTML+=( " + "第" +i+ "题答案:" +strs[1]); break ; case 1: var B=pl_f(); var strs = B.split( "=" ); document.getElementById( "text" ).innerHTML+=(strs[0]+ "=" + " ); document.getElementById( "text2" ).innerHTML+=( " + "第" +i+ "题答案:" +strs[1]); break ; } } }
|
以上就是本文的全部内容,希望对大家的学习有所帮助
jsp复习资料汇总
[JSP]2017年1月24日asp教程编程辅导汇总
[ASP]2016年12月2日JSP快速入门教程汇总
[JSP]2016年12月2日jsp基本用法和命令汇总
[JSP]2016年10月3日ASP编码教程:如何实现/使用缓存
[ASP]2015年4月15日