-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbreak.rb
302 lines (275 loc) · 4.57 KB
/
break.rb
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
def test
for i in 1..5
if i >3 then
break
end
puts "局部变量为#{i}"
end
for j in 33..77
if j >40
next
end
puts "#{j}是字母吗?"
end
j = 100
k = 300
l = 200
return j,k,l
end
puts test
def mount
for j in 33..99
if j > 55
next
end
puts "is not #{j}"
end
for i in 1..109
if i > 5 then
break
end
puts "局部变量是#{i}"
end
a = 299
b = 999
c = 000
return a,b,c
end
puts mount
def age
for i in 2..28
if i > 4 then
break
end
puts "#{i}是局部变量"
end
for j in 22..100
if j < 55
next
end
puts "is #{j}"
end
lili = 89
cici = 8
loyo = 6
return lili,cici,loyo
end
puts age
def good
for j in 3..79
if j >= 5 then
break
end
puts "#{j}是局部变量"
end
for i in 33..55
if i < 36
next
end
puts "#{i}是数字 啊"
end
price = 879
name = 1770
return price,name
end
puts good
def time
for u in 4..88
if u >= 5
break
end
puts "#{u}是局部变量"
end
for i in 4..44
if i > 22
next
end
puts " 变量是#{i}"
end
today = 3.30
yestoday =3.29
tomorrow = 3.31
return today,yestoday,tomorrow
end
puts time
def name
for i in 4..44
if i >= 7 then
break
end
puts "#{i}是局部变量"
end
for j in 4..55
if j < 7
next
end
puts "#{j}小于7"
end
firstname = "jinping"
lastname = "xi"
return firstname,lastname
end
puts name
def book
for i in 33..55
if i > 44 then
break
end
puts "#{i}happy"
end
for j in 44..55
if j > 55
next
end
puts "#{j}hhhh"
end
name = "三体"
price = 299
author = "大刘"
return name,price ,author
end
puts book
def game
for i in 33..66
if i > 35 then
break
end
puts "#{i}hihi"
end
for j in 56..66
if j > 66
next
end
puts "#{j}<=66"
end
name = "摩尔庄园"
type = "休闲养成"
return name,type
end
puts game
def hobby
for i in 22..44
if i >33
break
end
puts "#{i}hhhh"
end
for j in 33..66
if j >44
next
end
puts "#{j}小于等于44"
end
name = "baseball"
type = "sports"
return name,type
end
puts hobby
def songs
for i in 33..44
if i > 36
break
end
puts "#{i}是数字"
end
for j in 33..55
if j >44
break
end
puts "#{j}>44"
end
name = "sunshine"
type = "Japaness"
return name,type
end
puts songs
def color
for i in 44..55
if i > 45
break
end
puts "#{i}hhhahah"
end
for j in 33..55
if j >44
next
end
puts "变量是#{j}"
end
red = "red"
blue = "blue"
return red,blue
end
puts color
#判断某个数是否是质数
def is_prime_number n
#如果n为1 ,则n为质数,返回true
if n == 1
puts "-------n= 1 返回true"
return true
end
# 设置变量i,初始值为2,对n以内数值进行遍历
i = 2
loop do
puts "------------------i is #{i} n is #{n}"
#如果i % n 等于0 且 i不是n和i自身。返回false,不是质数
if n % i == 0 && i != 1 && i != n
puts " -----------n>2=== n is #{n} i is #{i} n % i = #{n % i } 返回false"
return false
end
#让i加一,进行下一个元素的循环
i = i + 1
puts "==========i is #{i}"
if i > n
break
end
end
#否则,返回true,也就是i为质数
return true
end
#puts prime_number 4
#获取n以内的质数的和
def get_all_prime_number n
puts "-----------n is #{n}----"
result = []
i = 1
loop do
result.push(i) if is_prime_number i
i = i + 1
puts "----i is #{i}----n is #{n}----"
if i > n
break
end
end
return result
end
#puts get_all_prime_number 7
#获取n以内的质数的和,奇数位置相加,偶数位置相减
def sum_special n
#定义初始值sum为0
sum = 0
#获取n以内的所有质数,调用数组方法,进行遍历
all_prime_number = get_all_prime_number n
i = 0
#使用loop do的方法
loop do
puts "----i is #{i}-----n is #{n}----"
prime_number = get_all_prime_number i
if (i + 1) % 2 == 0
puts "i is #{i}-----#{i + 1} is i+1 ----(i + 1) % 2 is#{(i + 1) % 2 }"
puts "sum is #{sum}.......sum - i is #{sum - i}...."
sum = sum + i
else
puts "sum is #{sum}.....sum + i is #{sum + i}....."
sum = sum - i
end
puts ".....i is #{i}......i + 1 is #{i + 1}"
i = i + 1
puts ",,,,,,i is #{i}......n is #{n}....."
if i > n
break
end
end
return sum
end
puts sum_special 7