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

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

结果:
在这里插入图片描述

希望对你们有帮助

  • 9
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值