#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int last;
do{
system("cls");
cout<<" FIBONACCI "<<endl;
int a,sum;
cout<<"Enter the number of outputs you want to be displayed : ";
cin>>a;
long long unsigned int b= 0, c=1;
while(a >=0)
{
cout<<b<<endl;
sum+=b;
b=b+c;
c=b-c;
a--;
}
cout<<"total = "<<sum<<endl;
cin>>last;
}
while(last==0);
system("pause");
return 0;
}
每当我想通过给出0作为最后一个输入来重复它时,sum的值不会自动重置,并且新的和被添加到前一个并且错误的值显示为总数。
答案 0 :(得分:3)
您的变量总和在您访问之前未初始化。
你应该改变
int a,sum;
到
int a;
int sum = 0;
答案 1 :(得分:1)
您已声明K1 i a p
错误,其类型应与class SomeClass a where
someFun :: a -> Maybe Int
default someFun :: (Generic a, GSomeClass (Rep a)) => a -> Maybe Int
someFun a = gsomeFun (from a)
和sum
b
和c
也没有被初始化,但是:你的计算很奇怪
long long unsigned int sum;
所以我建议
sum
答案 2 :(得分:0)
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int last;
do{
system("cls");
cout<<"FIBONACCI \n\n";
int a,sum;
cout<<"Enter the number of outputs you want to be displayed : ";
cin>>a;
long long unsigned int sum=0,b=0,c=1,d=0;
while(a >=0)
{
cout<<b<<endl;
sum+=b;
b=d+c;
c=d;
d=b;
a--;
}
cout<<"total = "<<sum<<endl;
cin>>last;
}
while(last==0);
system("pause");
return 0;
}
在计算中进行了更改,并且在do..while循环的求和中将sum初始化为零并且对不良标点符号抱歉