【问题标题】:curly brackets - recursion花括号 - 递归
【发布时间】:2015-04-18 18:06:01
【问题描述】:

我已经阅读了一些关于使用大括号的先前问题,据我了解,如果只有一行,则可以不使用大括号,但如果要使用多行代码,则需要使用括号.

我有一个作业,教练确实要求我们在每种情况下都使用括号来养成良好的习惯。他还允许我们研究和使用示例代码。

现在,我的问题是,我发现了一些不使用括号的示例代码。当我尝试将括号添加到代码中时,它会使我的输出不正确。 有人可以向我解释如何在多行代码中正确使用大括号,并就如何实现我想要的结果提出建议。

输出正确时的代码如下:

void printStars(int i, int n)
// The calling program invokes this function as follows: printStars(1, n);
// n >= 1
{ 
if(i == n)
{
    for(int j = 1; j <= n; j++) cout << '*'; cout << endl;
    for(int j = 1; j <= n; j++) cout << '*'; cout << endl;
} 
    else
    {
        for(int j = 1; j <= i; j++) cout << '*'; cout << endl;
        printStars(i+1, n); // recursive invocation

        for(int j = 1; j <= i; j++) cout << '*'; cout << endl;
    }
} // printStars

int main() {
    int n;
    int i=0;

        cout << "Enter the number of lines  in the grid: ";
        cin>> n;
        cout << endl;

        printStars(i,n);

    return 0;
}

当我尝试“清理”时,看起来像这样:

void printStars(int i, int n)
// The calling program invokes this function as follows: printStars(1, n);
{
    if(i == n)
    {
        for(int j = 1; j <= n; j++)
        {
            cout << '*';
            cout << endl;
        }
        for(int j = 1; j <= n; j++)
        {
            cout << '*';
            cout << endl;
        }
    }
    else
    {
        for(int j = 1; j <= i; j++)
        {
            cout << '*';
            cout << endl;
        }
        printStars(i+1, n); // recursive invocation

        for(int j = 1; j <= i; j++)
        {
            cout << '*';
            cout << endl;
        }
    }
} // printStars

int main() {
    int n;
    int i=0;

        cout << "Enter the number of lines  in the grid: ";
        cin>> n;
        cout << endl;

        printStars(i,n);

    return 0;
}

【问题讨论】:

  • 请记住,语句以分号 (;) 结尾。
  • 是的,我缺一个吗?
  • 编译器不会介意您将多个语句放在一行中。但是,如果您在单个语句周围添加大括号,则不应将其添加到同一行的第二个语句中。
  • 好的,我明白你在说什么谢谢你的意见。

标签: c++ data-structures curly-brackets


【解决方案1】:

问题是你在打印循环中投入了太多:

    for(int j = 1; j <= i; j++)
    {
        cout << '*';
        cout << endl;
    }

应该是:

    for(int j = 1; j <= i; j++)
    {
        cout << '*';
    }
    cout << endl;

没有花括号的循环只能包含一个单个语句。这意味着使用cout 的行尾打印仅在循环结束时调用。

这是使用花括号的完整代码:

void printStars(int i, int n)
// The calling program invokes this function as follows: printStars(1, n);
// n >= 1
{ 
if(i == n)
{
    for(int j = 1; j <= n; j++){
        cout << '*';
    }
    cout << endl;

    for(int j = 1; j <= n; j++){
        cout << '*';
    }
    cout << endl;
} 
    else
    {
        for(int j = 1; j <= i; j++){
            cout << '*';
        }
        cout << endl;

        printStars(i+1, n); // recursive invocation

        for(int j = 1; j <= i; j++){
            cout << '*';
        }
        cout << endl;
    }
} // printStars

int main() {
    int n;
    int i=0;

        cout << "Enter the number of lines  in the grid: ";
        cin>> n;
        cout << endl;

        printStars(i,n);

    return 0;
}

【讨论】:

  • 好的,所以 endl 必须在循环之外?顺便说一句,它工作得很好,谢谢!
  • 好吧,如果我们同意没有花括号的 for 循环仅适用于单个语句,我们可以理解第二个语句(cout &lt;&lt; endl)不是循环的一部分。祝你好运。
  • 谢谢,我对堆栈溢出还很陌生,我该如何评价?我认为我没有足够的“声誉”来投票
  • 我得到一个否定 2 我的问题是不是一个不好的问题?我觉得我在提问之前确实研究过这个问题......
  • 感谢您向我发送该链接。我会更了解如何正确提问。
猜你喜欢
  • 2018-09-12
  • 2023-04-09
  • 2019-01-26
  • 2012-01-18
  • 2018-05-02
  • 2013-11-18
  • 1970-01-01
  • 1970-01-01
  • 2022-12-04
相关资源
最近更新 更多