如何使用循环计算数字 [英] How do I count numbers using loops

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

问题描述

你好,

i想要制作一个从左到右计算数字的程序。

我不确定使用哪个循环以及如何使用,我在考虑在for循环语句中使用for循环打印到网页(document.write();)



我尝试过:



hello,
i want to make a program that counts numbers from left to right.
i'm not sure which loops to use and how, i was thinking of using a for loop in a for loop statement that prints to the webpage (document.write();)

What I have tried:

for(var i=0;i <=10;i++){
   for(var i=0;i <=10;i++){
     document.write('the number is: ' + i);
    }
}

推荐答案

不确定您想要计算的数字,但总循环次数是在您的情况下:



Not sure what numbers you exactly want to count, but the total loop count is in your case:

for(var i=0;i <=10;i++){
   for(var j=0;j <=10;j++){
     document.write('the number is: ' + (i + j));
    }
}





您的初始代码snipet看起来不对,因为您使用 i <外部和内部循环中的/ code>表示您的循环永远不会结束。您甚至声明 i 两次weach意味着您的运行时环境应该抛出错误而不执行代码...



如果能够更好地解释想要计算的数字,以便更好地了解如何计算它们,那将是一件好事。



Your initial code snipet looks wrong because you use i in the outer and inner loop which means that your loops will never end. You even declare i twice weach means that your runtime environment should throw an error without executing the code...

It would be good if you could explain better what numbers you want to count in order to get a better suggestion on how to count them ...


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

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