1.判断输入数是否为奇数
#include<iostream>
using namespace std;
void main()
{
int input;
cout << "请输入整数" << endl;
cin >> input;
if (input % 2 != 0)
{
cout << "输入为奇数" << endl;
}
system("pause");
}
结果:
2.根据分数判断是否优秀
#include<iostream>
using namespace std;
void main()
{
int input;
cout << "请输入分数" << endl;
cin >> input;
if (input > 90)
{
cout << "优秀" << endl;
}
else
{
cout << "需要努力了" << endl;
}
system("pause");
}
结果:
3改进奇偶性判别
#include<iostream>
using namespace std;
void main()
{
int input;
cout << "请输入数值" << endl;
cin >> input;
if (input%2==0)
{
cout << "偶数" << endl;
}
else
{
cout << "奇数" << endl;
}
system("pause");
}
结果:
4.根据成绩划分等级
#include<iostream>
using namespace std;
void main()
{
int input;
cout << "请输入数值" << endl;
cin >> input;
if (input>=90)
{
cout << "优秀" << endl;
}
else if(input>=60&&input<90)
{
cout << "合格" << endl;
}
else
{
cout << "需要努力了" << endl;
}
system("pause");
}
结果:
5.用条件运算符完成判断数奇偶性
#include<iostream>
using namespace std;
void main()
{
int input;
cout << "输入数值" << endl;
cin >> input;
(input % 2 == 0) ? (cout << "偶数" << endl) : (cout << "奇数" << endl);
system("pause");
}
结果:
6.用条件表达式判断一个数是否为3和5的整倍数
#include<iostream>
using namespace std;
void main()
{
int input;
cout << "输入数值" << endl;
cin >> input;
(input % 3== 0&& input % 5==0) ? (cout << "是" << endl) : (cout << "否" << endl);
system("pause");
}
结果:
7用条件表达式判断一个数是否为3和5的整倍数(嵌套)
#include<iostream>
using namespace std;
void main()
{
int input;
cout << "输入数值" << endl;
cin >> input;
(input % 3 == 0) ?
((input % 5 == 0) ? cout << "是" << endl : cout << "否" << endl)
:cout << "否" << endl;
system("pause");
}
结果:
8.根据输入的字符输出字符串
#include<iostream>
using namespace std;
void main()
{
char input;
cout << "输入数值" << endl;
cin >> input;
switch (input)
{
case 'a':
cout<< "very good" << endl;
break;
case 'b':
cout << "good" << endl;
break;
case 'c':
cout << "normal" << endl;
break;
case 'd':
cout << "failure" << endl;
break;
default:
cout << "input error" << endl;
}
system("pause");
}
结果:
9根据输入的字符输出字符串
#include<iostream>
using namespace std;
void main()
{
char input;
cout << "输入字符" << endl;
cin >> input;
if (input == 'a')
{
cout << "very good" << endl;
system("pause");
return; //return是跳出主函数,所以每一条之前加一个system
}
if (input == 'b')
{
cout << "good" << endl;
system("pause");
return;
}
if (input == 'c')
{
cout << "normal" << endl;
system("pause");
return;
}
if (input == 'd')
{
cout << "failure" << endl;
system("pause");
return;
}
cout << "input error" << endl;
system("pause");
}
结果:
10.根据输入的字符输出字符串
#include<iostream>
using namespace std;
void main()
{
char input;
cout << "输入字符" << endl;
cin >> input;
if (input == 'a')
{
cout << "very good" << endl;
system("pause");
return; //return是跳出主函数,所以每一条之前加一个system
}
else if (input == 'b')
{
cout << "good" << endl;
system("pause");
return;
}
else if (input == 'c')
{
cout << "normal" << endl;
system("pause");
return;
}
else if (input == 'd')
{
cout << "failure" << endl;
system("pause");
return;
}
else
{
cout << "input error" << endl;
system("pause");
}
}
结果:
#include<iostream>
using namespace std;
void main()
{
int input;
cout << "输入" << endl;
cin >> input;
switch(input) //没有break,会顺序执行完。
{
case 1:
cout << "Monday" << endl;
case 2:
cout << "Tuesday" << endl;
case 3:
cout << "Wednesday" << endl;
case 4:
cout << "Thursday" << endl;
case 5:
cout << "Friday" << endl;
case 6:
cout << "Saturday" << endl;
case 7:
cout << "Sunday" << endl;
default:
cout << "input error" << endl;
}
system("pause");
}
结果:
12.判断语句的嵌套(判断是否为闰年)
#include<iostream>
using namespace std;
void main()
{
int year;
cin >> year;
if (year % 4 == 0)
{
if (year % 400 == 0)
{
cout << "是" << endl;
}
else
cout << "不是" << endl;
}
else
cout << "不是" << endl;
system("pause");
}
结果:
12.判断是否为闰年
#include<iostream>
using namespace std;
void main()
{
int year;
cin >> year;
if (year%4 == 0&&year%400==0)
{
cout << "是" << endl;
}
else
cout << "不是" << endl;
system("pause");
}
结果:
希望对你们有帮助