为用户提供循环for循环的选项

Giving the user the option to loop a for loop

本文关键字:循环 for 选项 用户      更新时间:2023-10-16

我很难给用户一个循环选项。这个程序运行良好

#include <iostream>
using namespace std;
int main()
    {
    // goal is to calculate the sum of the first 10 terms of Leibniz's Series....
    // calculated by 1 - 1/3 + 1/5 - 1/7 + 1/9 ..... - 1/19
    int termNumber;  // keeps track of term numbers
    int numberOfTerms = 0;
    cout << "Enter number of Terms";
    cin >> numberOfTerms;

    double sum = 0.0;
    int sign = +1;
    for (termNumber = 1; termNumber <= numberOfTerms; termNumber++)
        {
        sum += ( sign / (2.0 * termNumber - 1));
        sign *= -1;
        }
    cout << "nn The sum is " << ( 4 * sum) << "nn";

    } // end body of loop

如果用户愿意的话,我需要给他一个重复程序的选项,所以我想我可以把它放在一个do while循环中,但当我这样做时,它只循环"输入术语数",无论我试图格式化它。这是我目前最好的。

  #include <iostream>
using namespace std;
int main()
{
// goal is to calculate the sum of the first 10 terms of Leibniz's Series....
// calculated by 1 - 1/3 + 1/5 - 1/7 + 1/9 ..... - 1/19
cout << "nGiven a positive integer specifying some number of terms, this programn approximates "
    "pi using Leibniz' Formula and the given number of terms.nn" ;
cout << "Leibniz' formula is 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ... = Pi / 4.nn";
char yes = 0;
double sum = 0.0;
do {
    int termNumber;  // keeps track of term numbers
    int numberOfTerms = 0;

    int sign = +1;
    cout << "enter number of terms.n";
    cin >> numberOfTerms;
    for (termNumber = 1; termNumber <= numberOfTerms; termNumber++)
        {
        sum += (sign / (2.0 * termNumber - 1));
        sign *= -1;
        }
    } 
    while (yes = 1);
cout << "nn The sum is " << (4 * sum) << "nn";
cout << "would you like to go again? " << yes;
} // end body of loop

我想让他们选择使用y/y和n/n有效的项数来尝试不同的数量。

谢谢你给我的任何帮助。

您需要一个cin来设置yes,而您的cout行位于错误的位置。它们需要在你的do-white循环中。和一个==在你的同时

do {
int termNumber;  // keeps track of term numbers
int numberOfTerms = 0;

int sign = +1;
cout << "enter number of terms.n";
cin >> numberOfTerms;
for (termNumber = 1; termNumber <= numberOfTerms; termNumber++)
    {
    sum += (sign / (2.0 * termNumber - 1));
    sign *= -1;
    }
cout << "nn The sum is " << (4 * sum) << "nn";
cout << "would you like to go again? " << yes;
cin >> yes
} 
while (yes == 1);

尝试

这只是我的两分钱,但也许你可以将这两种想法结合起来并使用函数。将上面do部分中的代码放入一个函数中,该函数的参数为项数。也可以在这个函数的主体中使用for循环。然后在for循环的外部调用另一个函数,该函数询问用户是否愿意继续,并为"terms"询问一个新的数字,如果用户提供了"y"或"y",则将其传递回第一个函数;如果用户提供的是"n"或"n",则可以停止程序

你学会使用了吗

continue;

在for循环中?我认为这符合你的描述,基本上是什么

continue;     

在这种情况下,在特定的场景中,它会返回到for循环的开头。

do while循环的问题在于,while()语句中使用的变量必须在循环体之外定义,即使在语法上属于循环。当使用for循环时可以避免这种情况,因为它允许在for语句中定义此类变量。下面是一个例子。

inline double leibniz (int n) noexcept   // function for Leibniz sum
{
  double result=0;
  for(int k=1,sign=1; n; --n,++++k,sign=-sign)
    result += double(sign)/double(k);
  return result;
};
int main()
{
                                      // no loose variables defined outside loop
  for(bool again=true; again; ) {     // control variable only lives withing loop body
    int n;                            // number of terms, only needed within loop body
    std::cout<<"number of terms = ";
    std::cin >> n;
    std::cout<<"result = "<<leibniz(n)<<'n'
             <<"try again? (1/0)";
    std::cin >>again;
  }
}

感谢大家的帮助。

我还不知道continue;,但我了解了它。

主要问题是我的cincout没有像之前所说的那样处于正确的位置。将最后一行修改为while (yes == 'Y' || yes == 'y');,现在一切都很完美。

你们太棒了!