一,写出C++语句声明一个常量PI,值为3.1416;再声明一个浮点型变量a,把PI的值赋值给a
const float PI=3.1416;
flaot a=PI;
二,写一条for语句,计数条件为n从100到200,步长为2;然后用while 和do-while语句完成同样的循环
- for循环
for(int n=100;n<=200;n+=2)
- while循环
int n=100;
while(n<=200)
n+=2;
- do-while循环
int n=100;
do
{n+=2;
}
while(n<=200);
三,编写一个程序,运行时提示一个数字,再把这个数字显示出来
#innclude<iostream>
using namespace std;
int main()
{int n=0;
cout<<"输入一个数字:";
cin>>n;
cout<<"你输入的是"<<n<<endl;
return 0;
}
四,打印ASCII码32~127字符
#include<iostream>
using namespace std;
int main()
{for(int i=32;i<=127;i++)
{char n=(char)i;
cout<<n<<endl;
}
return 0;
}
五,编写一个完整的程序,实现功能:向用户提问“现在正在下雨吗?”,提示用户输入Y或N。若输入为Y,显示“现在正在下雨。”;若输入为N,显示“现在没有下雨。”;否则继续提问“现在正在下雨吗?”。
#include<iostream>
using namespace std;
int main()
{char in;
while(1)
{cout<<"现在正在下雨吗?(Y or N):";
cin>>in;
if(in=='Y')
cout<<"现在正在下雨。"<<endl;
break;
if(in=='N')
cout<<"现在没有下雨。"<<endl;
break;
}
return 0;
}