我必须制定一个计算每年国家人口的计划。 2014年人口为x,2015年为x *(12%),每年增加12%。 我试过这样做,但无法通过它:
#include<iostream>
using namespace std;
int main(){
int year;
double pop=344000
cout<<"Year of population: ";
cin >> year;
switch(year){
case 2014: cout<< "344000\n";
break;
case 2015: cout<< pop+=* 0.12 + pop ; //last year pop *0.12+ last year pop
break;
cout <<year;
}
system("pause");
return year;
}
我知道它很乱,但我真的是c ++中的noob
答案 0 :(得分:0)
将明年的人口增加到最后一个人口的12%,我们检查过了多少年,并且不需要 切换
int main(){
int year;
int baseYear=2014;
int dif;
int population=2000; //number of people in 2014
cout<<"Year of population: ";
cin >> year;
dif=year-baseYear
for(int i=0;i<dif;i++)
population+= ((0.12)*population)
cout << population;
}
答案 1 :(得分:0)
我希望看一下这段代码可以帮助你理解你所缺少的东西。
include<iostream.h>
void main()
{
int population= 3000000;
int year;
cout<<"Enter a year greater than or equal to 2014 ";
cin>>year;
if(year<2014)
cout<<"year must be greater than or equal to 2014";
elseif(year==2014)
cout<<population;
else
{
int i=2014;
while(i<=year)
{
population=population*1.12;
i++;
}
cout<<"population";
}
}