在 ArnoldC 中编写嵌套循环?
Writing nested loop in ArnoldC?
ArnoldC 是一种深奥的语言,它使用阿诺德施瓦辛格的电影台词(对话)。它可以用来做很多基本的程序。我试图在其中编写一个嵌套循环,但无法创建它。是否可以在 ArnoldC 中编写嵌套循环?
当然可以。您只需要 在外部声明所有循环变量 (您不能在其中声明它们),并且 每轮初始化它们 (在条件变量之后成功)。然后你可以在外部 CHILL
调用之前嵌套循环。
我将提供一个使用计数器循环的示例,它建立在一个标志变量和一个计数器变量的基础上,并将两个计数器打印为一个四位数。注释在 #
之后,注意它们不能在代码中(你可以 Try it online!):
IT'S SHOWTIME
# declare outer and inner loop flags
HEY CHRISTMAS TREE loop
YOU SET US UP @NO PROBLEMO
HEY CHRISTMAS TREE innerloop
YOU SET US UP @NO PROBLEMO
# declare outer and inner loop counters
HEY CHRISTMAS TREE counter
YOU SET US UP @I LIED
HEY CHRISTMAS TREE innercounter
YOU SET US UP @I LIED
# declare variable for calculations
HEY CHRISTMAS TREE calc
YOU SET US UP @I LIED
# outer loop, using `loop` and `counter`
STICK AROUND loop
# decrease outer counter
GET TO THE CHOPPER counter
HERE IS MY INVITATION counter
GET UP 1
ENOUGH TALK
# prepare inner loop variables - set flag to TRUE and counter to 0
GET TO THE CHOPPER innerloop
HERE IS MY INVITATION @NO PROBLEMO
ENOUGH TALK
GET TO THE CHOPPER innercounter
HERE IS MY INVITATION @I LIED
ENOUGH TALK
STICK AROUND innerloop
# decrease inner counter
GET TO THE CHOPPER innercounter
HERE IS MY INVITATION innercounter
GET UP 1
ENOUGH TALK
# calculate outer counter * 100 + inner counter
GET TO THE CHOPPER calc
HERE IS MY INVITATION counter
YOU'RE FIRED 100
GET UP innercounter
ENOUGH TALK
# print it
TALK TO THE HAND calc
# has inner counter reached 10? if so, set loop variable FALSE
GET TO THE CHOPPER innerloop
HERE IS MY INVITATION 10
LET OFF SOME STEAM BENNET innercounter
ENOUGH TALK
CHILL
# has outer counter reached 10? if so, set loop variable FALSE
GET TO THE CHOPPER loop
HERE IS MY INVITATION 10
LET OFF SOME STEAM BENNET counter
ENOUGH TALK
CHILL
YOU HAVE BEEN TERMINATED
ArnoldC 是一种深奥的语言,它使用阿诺德施瓦辛格的电影台词(对话)。它可以用来做很多基本的程序。我试图在其中编写一个嵌套循环,但无法创建它。是否可以在 ArnoldC 中编写嵌套循环?
当然可以。您只需要 在外部声明所有循环变量 (您不能在其中声明它们),并且 每轮初始化它们 (在条件变量之后成功)。然后你可以在外部 CHILL
调用之前嵌套循环。
我将提供一个使用计数器循环的示例,它建立在一个标志变量和一个计数器变量的基础上,并将两个计数器打印为一个四位数。注释在 #
之后,注意它们不能在代码中(你可以 Try it online!):
IT'S SHOWTIME
# declare outer and inner loop flags
HEY CHRISTMAS TREE loop
YOU SET US UP @NO PROBLEMO
HEY CHRISTMAS TREE innerloop
YOU SET US UP @NO PROBLEMO
# declare outer and inner loop counters
HEY CHRISTMAS TREE counter
YOU SET US UP @I LIED
HEY CHRISTMAS TREE innercounter
YOU SET US UP @I LIED
# declare variable for calculations
HEY CHRISTMAS TREE calc
YOU SET US UP @I LIED
# outer loop, using `loop` and `counter`
STICK AROUND loop
# decrease outer counter
GET TO THE CHOPPER counter
HERE IS MY INVITATION counter
GET UP 1
ENOUGH TALK
# prepare inner loop variables - set flag to TRUE and counter to 0
GET TO THE CHOPPER innerloop
HERE IS MY INVITATION @NO PROBLEMO
ENOUGH TALK
GET TO THE CHOPPER innercounter
HERE IS MY INVITATION @I LIED
ENOUGH TALK
STICK AROUND innerloop
# decrease inner counter
GET TO THE CHOPPER innercounter
HERE IS MY INVITATION innercounter
GET UP 1
ENOUGH TALK
# calculate outer counter * 100 + inner counter
GET TO THE CHOPPER calc
HERE IS MY INVITATION counter
YOU'RE FIRED 100
GET UP innercounter
ENOUGH TALK
# print it
TALK TO THE HAND calc
# has inner counter reached 10? if so, set loop variable FALSE
GET TO THE CHOPPER innerloop
HERE IS MY INVITATION 10
LET OFF SOME STEAM BENNET innercounter
ENOUGH TALK
CHILL
# has outer counter reached 10? if so, set loop variable FALSE
GET TO THE CHOPPER loop
HERE IS MY INVITATION 10
LET OFF SOME STEAM BENNET counter
ENOUGH TALK
CHILL
YOU HAVE BEEN TERMINATED