无法理解某些代码。

编程入门 行业动态 更新时间:2024-10-24 13:25:01
本文介绍了无法理解某些代码。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所以这是我们的老师今天教给我们的2个程序,但是无法弄清楚如何去做。 第一:

So here are 2 programs that our teacher taught us today but can't figure out how to do it. First:

void main() { for(int i=1; i<=5; ++i) { for(int j=1; j<=1; ++j) cout<<j<<""; cout<<'\n'; } }

所以它应该打印出来: 1 12 123 1234 12345 第二名:

So it should print this: 1 12 123 1234 12345 Second:

void main() { for(int i=5; i>=1; --i) { for(int j=1; j<=1; ++j) cout<<j<<""; cout<<'\n'; } }

这必须这样做: 12345 1234 123 12 1 如果你只能解释第一个,它也会很棒。我可以搞清楚第二个。

And this must do: 12345 1234 123 12 1 If you can only explain the first one, it will be great either. I can figure out the second one.

推荐答案

如果我修复它们,也许它们会更有意义吗? if i fix them, maybe they'll make more sense? void main() { for(int i=1; i<=5; ++i) { // was j<=1, should be j<=i for(int j=1; j<=i; ++j) { cout<<j<<""; } cout<<'\n'; } }

void main() { // was i<>=1, should be i!=1 for(int i=5; i!=1; --i) { // was j<=1, should be j<=i for(int j=1; j<=i; ++j) { cout<<j<<""; } cout<<'\n'; } }

您好, 您的两个代码均为错误。 Hi, Your both codes are wrong. void main() { for(int i=1; i<=5; ++i) { for(int j=1; j<=i; ++j) cout<<j<<""; cout<<'\n'; } }

在第二个for循环中,条件应包括i而不是1。 这里forst循环继续,在第二个循环中,值打印为1. 在第二次迭代中,i的值= 2. 所以它会打印12. 就像明智一样,它将继续打印,直到外循环条件满足。 如果可以的话能够学习干运行然后它会帮助你开发逻辑。不建议每次从这样的论坛获得帮助。 谢谢, Sisir Patro

In the second for loop the condition should include "i" not "1". Here the forst loop goes on and in the 2nd loop the values get printed as 1. In the 2nd iteration the value of i=2. So it will print 12. like wise it will go on printing till the outer loop condition satisfies. If you can able to learn dry run then it would help you in developing logic. its not advisable to get help every time from such forums. Thanks, Sisir Patro

更多推荐

无法理解某些代码。

本文发布于:2023-07-27 14:02:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222751.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!