嵌套While循环(帮助).. [英] Nested While Loop (help)..

查看:90
本文介绍了嵌套While循环(帮助)..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个打印星号的代码,第一行为1个星号,第二行为2个,第三行为3个等。

知道我经过多次尝试后,我感到非常沮丧。



请关注一下。 。 。 。 。

Hi I''m in a need to write a code which print asterisks in such a way that 1 asterisk for first line, 2 for second line, 3 for third line etc.
Know I''m fully frustrated after trying a lot of times.

Please have an eye. . . . .

#include <iostream>

using namespace std;

int main()
{
    int n = 5;
    int row = 0;
    int colum = 0;
    while(row<n)
    {
        
        while(colum<row)
        {
            cout << "*" << '\n';
            colum++;
        }
        row++;
    }
    return 0;
}

推荐答案

我通常不会做人的作业,但是你给了它一个拍摄,大多数人只是要求我们这样做。你犯了两个错误:



1 - 你在每个*之后发出换行符,你需要在每组* s之后发出换行符

2 - 你没有将colum重置为0,所以你的行永远不会变大。



我建议学习使用你的调试器,帮助您找到这两个问题。我不知道你正在使用什么编译器,但它们都支持断点AFAIK。



I don''t normally do people''s homework, but you''ve given it a shot, most people just ask us to do it. You''ve made two mistakes:

1 - you emit a newline after every *, you need to emit a newline after each group of *s
2 - you don''t reset colum to 0, so your rows never get any bigger.

I recommend learning to use your debugger, that should have helped you find both of these issues. I don''t know what compiler you''re using, but they all suppor breakpoints AFAIK.

#include <iostream>

using namespace std;
 
int main()
{
    int n = 5;
    int row = 0;
    int colum = 0;
    while(row<n)>
    {
        
        while(colum<row)>
        {
            cout << "*";
            colum++;
        }

		cout << "\n";
		colum = 0;
        row++;
    }
    return 0;
}</iostream>


在外部while循环中,您需要重置列的值。否则它只会在第一次通过内部。
Inside the outer while loop you need to reset the value of column. Otherwise it only goes through the inner while the first time.


这篇关于嵌套While循环(帮助)..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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