无法理解某些代码。 [英] Unable to understand certain codes.

查看:65
本文介绍了无法理解某些代码。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这是我们的老师今天教给我们的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


这篇关于无法理解某些代码。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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