【问题标题】:How to convert do while loop to while loop in C++?如何在 C++ 中将 do while 循环转换为 while 循环?
【发布时间】:2021-05-22 03:59:08
【问题描述】:

我编写了一个 C++ 程序,让用户使用do while 循环输入正数。尽管如此,当我尝试将 do while 循环转换为 while 循环时,预期的输出与 do while 循环不同。代码如下:

#include <iostream>
using namespace std;

int main()
{
    int n;

    do
    {
        cout << "Enter a non-negative integer: ";
        cin >> n;
        if (n < 0)
        {
            cout << "The integer you entered is negative. " << endl;
        }

    }
    while (n < 0);

    return 0;
}

终端要求用户重新输入数字,直到我编写的上述代码为正。但是,我尝试将do while循环转换为while循环如下图,根本没有输出。

我可以知道我写错了哪一部分吗?谢谢。

#include <iostream>
using namespace std;

int main()
{
    int n;

    while (n < 0)
    {
        cout << "Enter a non-negative integer: ";
        cin >> n;
        if (n < 0){
            cout << "The integer you entered is negative. " << endl;
         }
    }

    return 0;
}

【问题讨论】:

  • 在循环之前给 n 赋任何负值
  • 这能回答你的问题吗? Equivalent using for-loop instead do-while-loop
  • @Ch3steR 问题是输出不一样,因为我遵循了 google 和 youtube 的教程。
  • 在第二个代码示例中,由于n 未初始化,它可能恰好包含负数,也可能不包含。行为,特别是 while 循环条件在首次执行时是否为 true,未定义。

标签: c++ loops while-loop do-while control-structure


【解决方案1】:

在您的代码的while 版本中,您不知道n 的值是什么,因为在while (n&lt;0) 循环中使用它之前它没有被初始化。

int main()
{
    int n;
    // HERE YOU DO NOT KNOW IF n is NEGATIVE OR POSITIVE, YOU EITHER NEED TO INITIALIZE IT OR
    // ENTER IT by cin >> n
    while (n < 0)
    {
        cout << "Enter a non-negative integer: ";
        cin >> n;
        if (n < 0){
            cout << "The integer you entered is negative. " << endl;
         }
    }

    return 0;
}

然后,您需要重新排列一点以获得相同的输出,并使用循环外的n 的第一个输入。

例如,这将提供相同的输出:

#include <iostream>
using namespace std;

int main()
{
    int n=-1;
    cout << "Enter a non-negative integer: ";
    cin >> n;
    while (n < 0)
    {

        if (n < 0)
        {
            cout << "The integer you entered is negative. " << endl;
            cout << "Enter a non-negative integer: ";
            cin >> n;
        }
        else
        {
            // if positive you get out of the loop
            break;
        }
 
    }

    return 0;
}

【讨论】:

  • 欢迎。我尝试使用while 进行相同的循环。当然,这不是唯一的解决方案,但它很容易理解
【解决方案2】:

你必须在while(n

#include <iostream>
using namespace std;

int main()
{
    int n;
    cout << "Enter a non-negative integer: ";
    cin >> n;
    while (n < 0)
    {
        cout << "Enter a non-negative integer: ";
        cin >> n;
    if (n < 0)
            cout << "The integer you entered is "
                 << "negative. " << endl;
    }
    
    return 0;
}

或者你可以用负数初始化它:

#include <iostream>
using namespace std;

int main()
{
    int n=-1;
    
    while (n < 0)
    {
        cout << "Enter a non-negative integer: ";
        cin >> n;
    if (n < 0)
            cout << "The integer you entered is "
                 << "negative. " << endl;
    }
    
    return 0;
}

【讨论】:

  • 我明白了。所以我需要重复 cout 和 cin 语句两次以获得与 do while 循环相同的输出。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-18
  • 2018-11-21
  • 2020-08-28
相关资源
最近更新 更多