C++ 如何使用 do/while、While 语句或 For 语句查找用户输入的多个整数的总和
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21838582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find sum of several integers input by user using do/while, While statement or For statement
提问by Laila
Please help out here. I want to create a program whereby a user inputs several numbers (let's say 6 numbers from his/ her head). The program should then go ahead and calculate the sum of all these numbers. I however have to use a loop statement, either For statement, While statement or do/while statement. This is what I have so far:
请在这里帮忙。我想创建一个程序,让用户输入几个数字(假设从他/她的脑海中输入 6 个数字)。然后程序应该继续计算所有这些数字的总和。但是,我必须使用循环语句,For 语句、While 语句或 do/while 语句。这是我到目前为止:
#include <iostream>
using namespace std;
int main()
{
int count = 1;
int sum = 0;
int number;
int numberitems;
cout << "Enter number of items: \n";
cin >> numberitems;
cout << "Enter number: \n";
cin >> number;
do {
sum = sum + number;
count++;
} while (count <= 6);
}
AND HERE IS THE OTHER CODE USING WHILE STATEMENT
这是使用 while 语句的其他代码
#include <iostream>
using namespace std;
int main()
{
int count = 1;
int sum = 0;
int number;
cout << "Enter number: \n";
cin >> number;
while (count <= 6) {
sum = sum += number;
count++;
}
cout << sum << endl;
}
I know this is beginner stuff, well am a beginner so help out a sister politely. THANKS
我知道这是初学者的东西,我是初学者,所以请礼貌地帮助姐姐。谢谢
采纳答案by Laila
The FOR loop worked well, I modified it a tiny bit:
FOR 循环运行良好,我稍微修改了一下:
#include<iostream>
using namespace std;
int main ()
{
int sum = 0;
int number;
int numberitems;
cout << "Enter number of items: \n";
cin >> numberitems;
for(int i=0;i<numberitems;i++)
{
cout << "Enter number: \n";
cin >> number;
sum=sum+number;
}
cout<<"sum is: "<< sum<<endl;
}
HOWEVER, the WHILE loop has got some errors on line 11 (Count was not declared in this scope). What could be the issue? Also, if you would have a solution using DO,WHILE loop it would be wonderful. Thanks
但是,WHILE 循环在第 11 行出现了一些错误(Count 未在此范围内声明)。可能是什么问题?另外,如果你有一个使用 DO,WHILE 循环的解决方案,那就太好了。谢谢
回答by fzd
Do note that if you're adding stuff, you might always want to check that you're not going beyond the limits of int
(especially in homework exercises).
Also, int main ()
should return an int
.
请注意,如果您正在添加内容,您可能总是想检查您是否没有超出限制int
(尤其是在家庭作业练习中)。此外,int main ()
应该返回一个int
.
Using a "do .. while" loop:
使用“do .. while”循环:
#include<iostream>
using namespace std;
int main ()
{
int sum = 0;
int previous = 0;
int number;
int numberitems;
int count = 0;
cout << "Enter number of items: ";
cin >> numberitems;
if ( numberitems <= 0 )
{
//no request to perform sum
cout << "Quitting without summing.\n\n";
return 0;
}
do
{
cout << "Enter number to add : ";
cin >> number;
sum+=number;
// check here that the addition didn't break anything.
// Negative + negative should stay negative, positive + postive should stay positive
if ((number > 0 && previous > 0 && sum < 0) || (number < 0 && previous < 0 && sum > 0))
{
cout << "Error: Beyond int limits !!";
return 1;
}
count++;
previous = sum;
}
while ( count < numberitems);
cout<<"sum is: "<< sum<<endl;
return 0;
}
回答by lerato
#include<iostream>
int main()
{//initialize variables
int limit;
int num;
int sum=0;
int counter=0;
cout<<"Enter limit of numbers you wish to see"<<" ";
cin>>limit;
cout<<endl;
while(counter<limit)
{
cout<<"Enter number "<<endl;
cin>>num;
sum=sum+num;
counter++;
}
cout<<"The sum of numbers is "<<" "<<endl
return 0;
}
回答by David Z
A simple program shows how to use for loop to find sum of serveral integers.
一个简单的程序展示了如何使用 for 循环来查找多个整数的总和。
#include <iostream>
using namespace std;
int main ()
{
int sum = 0;
int endnum = 2;
for(int i = 0; i<=endnum; i++){
sum += i;
}
cout<<sum;
}
回答by John St
You should do:
你应该做:
#include<iostream>
using namespace std;
int main ()
{
int sum = 0;
int number;
int numberitems;
cout << "Enter number of items: \n";
cin >> numberitems;
for(int i=0;i<numberitems;i++)
{
cout << "Enter number <<i<<":" \n";
cin >> number; sum+=number;
}
cout<<"sum is: "<< sum<<endl;
}
And with a while statement
并有一个while语句
#include <iostream>
using namespace std;
int main ()
{
int sum = 0;
int number;
int numberitems;
cin>>numberitems;
cout << "Enter number: \n";
while (count <=numberitems)
{
cin >> number;
sum+=number;
}
cout << sum << endl;
}