本文介绍了帮助请在C ++中执行while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在此问题中使用do while循环?
how to use a do while loop in this problem?
#include<iostream.h>
#include<conio.h>
int main()
{
double a=50;
double b=70;
int x=1;
clrscr();
cout<<"Year Country A Country B";
cout<<"\n"<<x<<"\t"<<a<<"\t\t"<<b<<"\n";
while(a<b)
{
x++;
a=a*1.03;
b=b*1.02;
cout<<" "<<x<<"\t"<<a<<"\t\t"<<b<<"\n";
}
cout<<"\nCountry A's Population will surpass Country B's in "<<x<<"years";
getch();
return 0;
}
推荐答案
#include<iostream.h>
#include<conio.h>
int main()
{
double a=50;
double b=70;
int x=1;
clrscr();
cout<<"Year Country A Country B";
do
{
cout<<" "<<x<<"\t"<<a<<"\t\t"<<b<<"\n";
x++;
a=a*1.03;
b=b*1.02;
}
while(a<b);
cout<<"\nCountry A's Population will surpass Country B's in "<<x<<"years";
getch();
return 0 ;
}
这篇关于帮助请在C ++中执行while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!