C ++:执行do while循环重复

时间:2018-09-13 04:36:20

标签: c++ loops

对于超级新手问题,我感到非常抱歉,但是我一生都无法理解如何重复执行while循环。我将一个while循环更改为一个do while循环,但是现在我不知道如何问“您要重复Y还是N?”。有什么解释吗?

我读过完成do while循环重复的各种文章,但对我来说它们没有意义。 我本质上是否要将这段代码包装到另一个while循环中?我可以将布尔表达式移到一段时间之前吗?

#include <iostream>
using namespace std;
int main(void)
{
    int x;
    int count = 0;
    int N;
    double sum = 0; 
    double average;
    char ans;

    {
        cout << "Enter number of values, N, to be read in <Enter>:" << endl;
        cin >> N;
        do
        {
            cout << "\n Enter a grade <Enter>: ";
            cin >> x;
            sum = sum + x;
            count++; // 
        } while (count < N);

        if (N == 0)
            cout << "You have entered 0 numbers. No average will be computed. Bye! \n";
        else {
            average = average = sum / N;
            cout << "The average of these " << N << " grades is " << average << endl;
        }

         cout << "Would you like to enter more values to calculate your grade average?\n";

        system("pause");
        return 0;
    }
}

3 个答案:

答案 0 :(得分:0)

可能正是您所需要的,对于char YorN,您正在考虑是继续还是破坏内部do-while

#include <iostream>
using namespace std;
int main(void)
{
    int x;
    int count = 0;
    int N;
    double sum = 0; 
    double average;
    char ans;
    char YorN;

    do{
        cout << "Enter number of values, N, to be read in <Enter>:" << endl;
        cin >> N;
        do
        {
            cout << "\n Enter a grade <Enter>: ";
            cin >> x;
            sum = sum + x;
            count++; // 
        } while (count < N);

        if (N == 0)
            cout << "You have entered 0 numbers. No average will be computed. Bye! \n";
        else {
            average = average = sum / N;
            cout << "The average of these " << N << " grades is " << average << endl;
        }

        cout << "Would you like to enter more values to calculate your grade average?\n";
        cin>>YorN;

    } while (YorN=='Y');
    return 0;
}

答案 1 :(得分:0)

可以做到这一点:

 char repeat='y'; 
 cout << "Enter number of values, N, to be read in <Enter>:" << endl;
 cin >> N;

 do
    {
        for(int i=0;i<n;i++){
            cout << "\n Enter a grade <Enter>: ";
            cin >> x;
            sum = sum + x;
            count++; // 
        }

    if (N == 0)
        cout << "You have entered 0 numbers. No average will be computed. Bye! \n";
    else {
        average = average = sum / N;
        cout << "The average of these " << N << " grades is " << average << endl;
    }

     cout << "Would you like to enter more values to calculate your grade average?\n";
     cin>>repeat;

 }while(repeat=='y');

答案 2 :(得分:0)

尽管有很多方法可以更好地做到这一点,但是这段代码使我能够解释正在发生的事情。做循环绝对是一件很棘手的事情。但是,请记住,do-while循环旨在运行 至少一次

对于我们的代码,我们设置了if语句以在不合适的地方对正在运行的代码进行测试。

看看第一个if语句是N > 0的情况,如果语句确实执行了,请不要忘记这一点。再次强调,执行 if语句,只会导致错误。

话虽如此,当您希望代码块至少执行一次时,可以使用do-while循环。请注意,我们的while语句如何进行两项测试,一是用户继续进行的回答'Y',如果是,则新输入的N值是否更大大于0?

您可能想知道最里面的if语句是否至少执行一次,好答案是,它取决于先前的if语句结果if(N > 0)if(count == N && N != 0)每次{ {1}}循环保持为真。但是,嵌套的while取决于它的父结果。

我希望这可以在do-while循环中为您清除一些麻烦。

if