我需要知道如何在 C++ 中使用增量值增加到一定数量
I need to know how to add using an increment value up to a certain number in c++
我很难尝试将起始值与增量值相加,直到达到结束值或者无法再次添加,因为它会超过最大值(结束值)。
好的,我要开门见山了,这是我的作业。
在此作业中,您将完成一个 C++ 程序,该程序对一系列值中的整数求和并打印结果。这将通过两种不同的方式完成:使用 while 循环和使用 for 循环。
对于此作业,您可以更自由地选择需要声明的局部变量以及确定要编写的源代码。但是,您的程序需要遵循本课程的编码标准并满足下一节中描述的软件要求。
下面是程序的执行示例。在本例中,程序将数字相加 8, 25, 42, 59, 76, 93, and 110
。您的程序应遵循下面显示的相同格式来提示用户和打印结果。
输入起始整数值:8
输入结束整数值:121
输入正增量:17
总和(使用 while 循环):413
总和(使用 for 循环):413
这是我到目前为止的代码
#include <iostream>
using namespace std;
int main(){
//while loop sum
int sumw = 0;
//for loop sum
int sumf = 0;
//starting integer
int nums;
//ending integer
int nume;
//increment integer
int numi;
cout <<"Please enter a starting value: " << endl;
cin >> nums;
cout <<"Please enter an ending value: " << endl;
cin >> nume;
cout <<"Please enter a positive increment value: " << endl;
cin >> numi;
if (numi <= 0 || nums > nume) cout << "Error ";
if (numi <= 0 || nums > nume) return 0;
for (int i = 1; i <= numi; i++){
sumf =+ numi;
}
cout << "Sum(using for loop): " << sumf;
return 0;
}
如果有人能帮我解决这个问题那就太好了!!!谢谢!!
这可能就是您要找的
for (int i = nums; i <= nume; i = (i + numi)){
sumf += i;
}
从 nums
开始,直到小于或等于 nume
并以 numi
的步长递增 i
,即 i = i + numi
此外,您还可以组合:
if (numi <= 0 || nums > nume) cout << "Error ";
if (numi <= 0 || nums > nume) return 0;
到
if (numi <= 0 || nums > nume){
cout << "Error ";
return 0;
}
在很好的参考资料中查看 for
语句的定义:
for (
初始化器, 终止条件, 增量)
第三个参数是增量。
你可以这样做:
for (int i = nums; i < nume; i = i + numi)
{
}
在循环中,您将了解需要求和的内容以及如何求和。
欢迎来到计算机科学和编程的世界:)
几点:
- 不清楚您需要什么帮助,发布您的作业不是获得帮助的好方法。对于您的下一个问题,请尝试将此作为如何提问的指南:https://whosebug.com/help/how-to-ask
- 这里是 C++ 中运算符的列表,以及如何使用它们,https://www.geeksforgeeks.org/operators-c-c/ 你会注意到 =+ 不在那里,因为它不是有效的运算符。
你所拥有的是:
“+=”:This operator is combination of ‘+’ and ‘=’ operators. This
operator first adds the current value of the variable on left to the
value on right and then assigns the result to the variable on the
left.
Example: (a += b)
can be written as (a = a + b)
假设起始数大于等于1(>=1)。
使用 while 循环:
#include <iostream>
using namespace std;
int main()
{
int totalSum = 0, startingNumber, endingNumber, positiveIncrement;
cout <<"Enter the starting number: " << endl;
cin >> startingNumber;
cout <<"Enter the ending number: " << endl;
cin >> endingNumber;
cout <<"Enter the positive increment: " << endl;
cin >> positiveIncrement;
if ((startingNumber <= 0) || (startingNumber > endingNumber))
{
cout<<"Error in input provided"<< endl;
return 0;
}
totalSum = startingNumber;
while ((startingNumber + positiveIncrement) <= endingNumber)
{
startingNumber += positiveIncrement;
totalSum += startingNumber;
}
cout << "Total Sum = " << totalSum;
return 0;
}
使用 for 循环:
#include <iostream>
using namespace std;
int main()
{
int totalSum = 0, startingNumber, endingNumber, positiveIncrement;
cout <<"Enter the starting number: " << endl;
cin >> startingNumber;
cout <<"Enter the ending number: " << endl;
cin >> endingNumber;
cout <<"Enter the positive increment: " << endl;
cin >> positiveIncrement;
if ((startingNumber <= 0) || (startingNumber > endingNumber))
{
cout<<"Error in input provided"<< endl;
return 0;
}
for ((totalSum = startingNumber);((startingNumber + positiveIncrement) <= endingNumber);(startingNumber += positiveIncrement))
{
totalSum += (startingNumber+positiveIncrement);
}
cout << "Total Sum = " << totalSum;
return 0;
}
我很难尝试将起始值与增量值相加,直到达到结束值或者无法再次添加,因为它会超过最大值(结束值)。
好的,我要开门见山了,这是我的作业。
在此作业中,您将完成一个 C++ 程序,该程序对一系列值中的整数求和并打印结果。这将通过两种不同的方式完成:使用 while 循环和使用 for 循环。
对于此作业,您可以更自由地选择需要声明的局部变量以及确定要编写的源代码。但是,您的程序需要遵循本课程的编码标准并满足下一节中描述的软件要求。
下面是程序的执行示例。在本例中,程序将数字相加 8, 25, 42, 59, 76, 93, and 110
。您的程序应遵循下面显示的相同格式来提示用户和打印结果。
输入起始整数值:8
输入结束整数值:121
输入正增量:17
总和(使用 while 循环):413
总和(使用 for 循环):413
这是我到目前为止的代码
#include <iostream>
using namespace std;
int main(){
//while loop sum
int sumw = 0;
//for loop sum
int sumf = 0;
//starting integer
int nums;
//ending integer
int nume;
//increment integer
int numi;
cout <<"Please enter a starting value: " << endl;
cin >> nums;
cout <<"Please enter an ending value: " << endl;
cin >> nume;
cout <<"Please enter a positive increment value: " << endl;
cin >> numi;
if (numi <= 0 || nums > nume) cout << "Error ";
if (numi <= 0 || nums > nume) return 0;
for (int i = 1; i <= numi; i++){
sumf =+ numi;
}
cout << "Sum(using for loop): " << sumf;
return 0;
}
如果有人能帮我解决这个问题那就太好了!!!谢谢!!
这可能就是您要找的
for (int i = nums; i <= nume; i = (i + numi)){
sumf += i;
}
从 nums
开始,直到小于或等于 nume
并以 numi
的步长递增 i
,即 i = i + numi
此外,您还可以组合:
if (numi <= 0 || nums > nume) cout << "Error ";
if (numi <= 0 || nums > nume) return 0;
到
if (numi <= 0 || nums > nume){
cout << "Error ";
return 0;
}
在很好的参考资料中查看 for
语句的定义:
for (
初始化器, 终止条件, 增量)
第三个参数是增量。
你可以这样做:
for (int i = nums; i < nume; i = i + numi)
{
}
在循环中,您将了解需要求和的内容以及如何求和。
欢迎来到计算机科学和编程的世界:) 几点:
- 不清楚您需要什么帮助,发布您的作业不是获得帮助的好方法。对于您的下一个问题,请尝试将此作为如何提问的指南:https://whosebug.com/help/how-to-ask
- 这里是 C++ 中运算符的列表,以及如何使用它们,https://www.geeksforgeeks.org/operators-c-c/ 你会注意到 =+ 不在那里,因为它不是有效的运算符。 你所拥有的是:
“+=”:This operator is combination of ‘+’ and ‘=’ operators. This operator first adds the current value of the variable on left to the value on right and then assigns the result to the variable on the left.
Example:
(a += b)
can be written as(a = a + b)
假设起始数大于等于1(>=1)。 使用 while 循环:
#include <iostream>
using namespace std;
int main()
{
int totalSum = 0, startingNumber, endingNumber, positiveIncrement;
cout <<"Enter the starting number: " << endl;
cin >> startingNumber;
cout <<"Enter the ending number: " << endl;
cin >> endingNumber;
cout <<"Enter the positive increment: " << endl;
cin >> positiveIncrement;
if ((startingNumber <= 0) || (startingNumber > endingNumber))
{
cout<<"Error in input provided"<< endl;
return 0;
}
totalSum = startingNumber;
while ((startingNumber + positiveIncrement) <= endingNumber)
{
startingNumber += positiveIncrement;
totalSum += startingNumber;
}
cout << "Total Sum = " << totalSum;
return 0;
}
使用 for 循环:
#include <iostream>
using namespace std;
int main()
{
int totalSum = 0, startingNumber, endingNumber, positiveIncrement;
cout <<"Enter the starting number: " << endl;
cin >> startingNumber;
cout <<"Enter the ending number: " << endl;
cin >> endingNumber;
cout <<"Enter the positive increment: " << endl;
cin >> positiveIncrement;
if ((startingNumber <= 0) || (startingNumber > endingNumber))
{
cout<<"Error in input provided"<< endl;
return 0;
}
for ((totalSum = startingNumber);((startingNumber + positiveIncrement) <= endingNumber);(startingNumber += positiveIncrement))
{
totalSum += (startingNumber+positiveIncrement);
}
cout << "Total Sum = " << totalSum;
return 0;
}