如何循环一组数字 [英] how to loop through a set of numbers

查看:83
本文介绍了如何循环一组数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,你知道你写一个从1到10输出数字的循环的方式,除了这次我怎么把这组数字像23 23 24 65 3 556 44 65 43 .... ...在代码中,以便我的输出会给我我想要的东西。我不想通过数组来做它还有其他方法吗?请帮助。谢谢

i have a problem, you know the way you write a loop that output a number from 1 to 10. except this time how do i put the set of numbers like 23 23 24 65 3 556 44 65 43....... inside a code so that that my output would give me what i want. i don't wanna do it through array is there any other way? pls help. thanks

推荐答案

如果你必须迭代一组预定义的数字,写在代码中,那么你别无选择:你必须使用一个容器(比如建议的数组)来存储它们。

另一种方法是从用户(或从输入文件)读取它们并逐步处理它们(尽管如此,再次将读取的数字存储在ant数组中将是一个处理问题的有效方法)。

另一种替代方案,如果符合您的需要,将使用随机数生成器在每次迭代时获得一个新数字。
If you have to iterate over a predefined set of numbers, written in the code, then you have no choice: you have to use a container (like the proposed array) to store them.
An alternative would be reading them from the user (or from an input file) and processing them incrementally (albeit, again, storing the read numbers in ant array would be an effective way to handle the problem).
Another alternative, if fits your needs, would be using the random number generator to get a fresh number at each iteration.


我会接受这个疯狂的混合挑战。



JavaScript跟随,因为每个人都有JS。



I'll take this crazy mixed up challenge.

JavaScript follows, because everybody got JS.

var numberSet = [ 23, 23, 24, 65, 3, 556, 44, 65, 43];
var randomIdx = (Math.floor(Math.random()*numberSet.length));
// run this 100 times just to see what values you get
for (x = 0;x < 100;x++)
{
  console.log(numberSet[randomIdx]);
  randomIdx = (Math.floor(Math.random()*numberSet.length));
}







现在你可以将numberSet更改为你想要的任何数字集合只需键入值后跟逗号即可。在随机生成器中使用该组的长度来确定它应该在控制台上打印的numberSet的哪个indice。



注意:使用F12来显示控制台。

注意:无偿地投票给这个答案,因为它是gen1us。



编辑:我为此创建了一个plunker:它非常酷cuz您可以在屏幕上看到这些值。

请查看: http:// plnkr。 co / edit / B7H3PDCcVhPs1Dt2L2aZ?p =预览 [ ^ ]



看起来:顶部是价值,底部是指标使用




Now you can change numberSet to be any set of numbers you want by simply typing in values with followed by a comma. The length of the set is used in the random generator to determine which indice of the numberSet it should print on the console.

Note: use F12 to show the console.
Note: Vote this answer UP gratuitously, because it are gen1us.

I created a plunker for this: it pretty cool cuz you can see the values on the screen.
Check it out at: http://plnkr.co/edit/B7H3PDCcVhPs1Dt2L2aZ?p=preview[^]

Looks like: top part is value, bottom part is indice used

24   556   556   65   44   43   24   44   44   65   3   44   44   3   3   65   43   65   23   43   65   556   44   3   23   65   23   43   23   24   65   44   23   556   23   43   65   24   44   44   3   3   43   43   23   43   23   23   43   556   65   556   65   3   3   65   24   556   24   556   24   556   65   65   24   23   65   556   44   43   65   23   24   23   3   23   23   65   24   65   44   3   24   65   23   65   3   65   3   23   23   24   65   23   556   23   65   65   65   43   
#########################

5   5   7   6   8   2   6   6   7   4   6   6   4   4   3   8   7   0   8   7   5   6   4   1   7   0   8   0   2   7   6   1   5   0   8   7   2   6   6   4   4   8   8   0   8   0   0   8   5   3   5   7   4   4   3   2   5   2   5   2   5   7   7   2   0   7   5   6   8   3   0   2   0   4   0   0   3   2   7   6   4   2   7   0   3   4   7   4   1   1   2   7   1   5   1   7   7   3   8   6 


这篇关于如何循环一组数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆