c++基础篇4(条件判断语句)

1.判断输入数是否为奇数

#include
using namespace std;
void main()
{
	int input;
	cout << "请输入整数" << endl;
	cin >> input;
	if (input % 2 != 0)
	{
		cout << "输入为奇数" << endl;
	}
	system("pause");
}

结果:
c++基础篇4(条件判断语句)_第1张图片

2.根据分数判断是否优秀

#include
using namespace std;
void main()
{
	int input;
	cout << "请输入分数" << endl;
	cin >> input;
	if (input > 90)
	{
		cout << "优秀" << endl;
	}
	else
	{
		cout << "需要努力了" << endl;
	}
	system("pause");
}

结果:
c++基础篇4(条件判断语句)_第2张图片

3改进奇偶性判别

#include
using namespace std;
void main()
{
	int input;
	cout << "请输入数值" << endl;
	cin >> input;
	if (input%2==0)
	{
		cout << "偶数" << endl;
	}
	else
	{
		cout << "奇数" << endl;
	}
	system("pause");
}

结果:
c++基础篇4(条件判断语句)_第3张图片

4.根据成绩划分等级

#include
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");
}

结果:
c++基础篇4(条件判断语句)_第4张图片

5.用条件运算符完成判断数奇偶性

#include
using namespace std;
void main()
{
	int input;
	cout << "输入数值" << endl;
	cin >> input;
	(input % 2 == 0) ? (cout << "偶数" << endl) : (cout << "奇数" << endl);
	system("pause");
}

结果:
c++基础篇4(条件判断语句)_第5张图片

6.用条件表达式判断一个数是否为3和5的整倍数

#include
using namespace std;
void main()
{
	int input;
	cout << "输入数值" << endl;
	cin >> input;
	(input % 3== 0&& input % 5==0) ? (cout << "是" << endl) : (cout << "否" << endl);
	system("pause");
}

结果:
c++基础篇4(条件判断语句)_第6张图片

7用条件表达式判断一个数是否为3和5的整倍数(嵌套)

#include
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");
}

结果:
c++基础篇4(条件判断语句)_第7张图片

8.根据输入的字符输出字符串

#include
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");
}

结果:
c++基础篇4(条件判断语句)_第8张图片

9根据输入的字符输出字符串

#include
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");
}

结果:
c++基础篇4(条件判断语句)_第9张图片

10.根据输入的字符输出字符串

#include
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");
	}
}

结果:
c++基础篇4(条件判断语句)_第10张图片

#include
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");
	
}

结果:
c++基础篇4(条件判断语句)_第11张图片

12.判断语句的嵌套(判断是否为闰年)

#include
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");
	
}

结果:
c++基础篇4(条件判断语句)_第12张图片

12.判断是否为闰年

#include
using namespace std;
void main()
{
	int year;
	cin >> year;
	if (year%4 == 0&&year%400==0)
	{
		cout << "是" << endl;
	}
	else
		cout << "不是" << endl;
	system("pause");
	
}

结果:
c++基础篇4(条件判断语句)_第13张图片

希望对你们有帮助

你可能感兴趣的:(c++)