我正在使用从头开始。我从用户那里获得了两个值,并且必须在这些值之间找到可被 2 和 3 整除的数字。如何在不使用数组的情况下计算这些数字(仅使用基本操作)?
问问题
1037 次
2 回答
2
如果您只需要计算这些数字,则不需要数组。只需遍历范围并计数:
于 2015-05-23T11:49:54.663 回答
0
这是您可以做的(请注意,我不擅长创建问题和变量)...
when flag clicked
ask (starting number is?) and wait
set (startrange) to (answer)
ask (ending number is?) and wait
set (endrange) to (answer)
set (counter) to (startrange)
set (divisibleby2) to (0)
set (divisibleby3) to (0)
set (divisibleby6) to (0)
repeat until counter = endrange
if (counter) mod 6 = 0
change (divisibleby6) by (1)
else
if (counter) mod 3 = 0
change (divisibleby3) by (1)
else
if (counter) mod 2 = 0
change (divisibleby2) by (1)
say (join (The number of numbers from the two inputs that are divisible by 2, 3 is) ((divisibleby2) + ((divisibleby3) + (divisibleby6)))
那么,为什么divisibleby6
需要变量呢?这是因为有些数字可以同时被 2 和 3 整除,这意味着如果更改代码,该数字将被记录两次。但是,如果您希望发生这种情况,这将是您的代码:
hen flag clicked
ask (starting number is?) and wait
set (startrange) to (answer)
ask (ending number is?) and wait
set (endrange) to (answer)
set (counter) to (startrange)
set (divisibleby2) to (0)
set (divisibleby3) to (0)
repeat until counter = endrange
if (counter) mod 3 = 0
change (divisibleby3) by (1)
if (counter) mod 2 = 0
change (divisibleby2) by (1)
say (join (The number of numbers from the two inputs that are divisible by 2, 3 is) ((divisibleby2) + (divisibleby3))
于 2019-10-04T13:21:40.323 回答