为用户提供循环for循环的选项
Giving the user the option to loop a for loop
我很难给用户一个循环选项。这个程序运行良好
#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;
,但我了解了它。
主要问题是我的cin
和cout
没有像之前所说的那样处于正确的位置。将最后一行修改为while (yes == 'Y' || yes == 'y');
,现在一切都很完美。
你们太棒了!
相关文章:
- 如何在C++中从两个单独的for循环中添加两个数组
- 为什么我的for循环不能正确获取argv
- 在基于范围的for循环中使用结构化绑定声明
- 通过for循环使用用户输入填充列表
- 使用for循环检查数组中的重复项
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 正在使用for循环创建QScatterSerie
- Python中的for循环与C++有何不同
- 在更改for循环的第三部分后,未使用for循环结果
- 在 for 循环中查找问题时遇到困难
- 嵌套for循环C++的问题(初学者)
- 如何用for循环在c++中生成单词三角形
- 如何在for循环中包含两个索引值的测试条件
- 带有多个独立参数的C++For循环
- C++ Python 循环"for i, num in enumerate(list):"版本
- C 多循环 for () 基础知识
- 没有条件值的 FOR 循环"for (int i = 1; ; i++)"无法正常工作
- 为用户提供循环for循环的选项
- 打破循环for循环