if条件语句
第四天
XMind 思维导图复习之前知识
数据类型-变量常量-运算符(表达式)-语句(顺序、分支、循环)-数组-函数
1.if语句格式
if(表达式)
{
语句
}
注意:
1.如果,表达式成立,只执行一条语句的话,可以省去花括号。如果表达式成立,需要执行的语句数量大于等于2条,必须把这些句语放在花括号中。
2.if的小括号后,不要加分号。
2. if...else...格式
if(表达式)
{
}
else
{
}
注意:同if
1.可以有if没有else,但是有else,前面必须有if.
2.else后面没有括号和条件表达式。
3.满足条件走if后的花括号,直接就忽略else
3.多分支
if... else if .... else if.....else
if(表达式1)
{
语句1;
}
else if(表达式2)
{
语句2;
}
...
else
{
语句n;
}
作业:年龄段;不同时间段的问候语;输入月份显示多少天。
4.if嵌套
if(表达式)
{
if(表达式)
{
}
}
else
{
if(表达式)
{
}
else
{
}
}
作业:
1.输入年份,月份显示这个月有多少天?
static void Main(string[] args)
{
//输入年份,月份显示这个月有多少天?
Console.WriteLine("请输入年份:");
int year = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入月份:");
int month = Convert.ToInt32(Console.ReadLine());
if (month==||month==||month==||month==||month==||month==||month==)
{
Console.WriteLine("这个月是31天");
}
else if (month==||month==||month==||month==)
{
Console.WriteLine("这个月是30天");
}
else if (month==)
{
if (year%==||(year%!=&&year%==))
{
Console.WriteLine("这个月是29天");
}
else
{
Console.WriteLine("这个月是28天");
} }
else
{
Console.WriteLine("输入月份有问题");
}
}
2.输入年份,月份,天,判断这个日期是否正确?(试着做)
static void Main(string[] args)
{
//输入年份,月份,天,判断这个日期是否正确?(试着做)
int year, month, day;
Console.WriteLine("输入年:");
year = Convert.ToInt32(Console.ReadLine());
if (year>=-&&year<=)
{
//年份正确
Console.WriteLine("输入月份:");
month = Convert.ToInt32(Console.ReadLine());
if (month>=&&month<=)
{
//月份正确
//月份正确,判断是大月,小月,2月
if (month==||month==||month==||month==||month==||month==||month==)
{
//判断天数是否正确
Console.WriteLine("输入天:");
day = Convert.ToInt32(Console.ReadLine());
if (day>=&&day<=)
{
//日期正确
Console.WriteLine("日期正确");
}
else
{
//日期错误
Console.WriteLine("日期错误");
}
}
else if (month==||month==||month==||month==)
{
Console.WriteLine("输入天:");
day = Convert.ToInt32(Console.ReadLine());
if (day >= && day <= )
{
//日期正确
Console.WriteLine("日期正确");
}
else
{
//日期错误
Console.WriteLine("日期错误");
}
} else
{
//二月,判断是否是闰年
if (year%==||year%!=&&year%==)
{ Console.WriteLine("输入天:");
day = Convert.ToInt32(Console.ReadLine());
if (day >= && day <= )
{
//日期正确
Console.WriteLine("日期正确");
}
else
{
//日期错误
Console.WriteLine("日期错误");
}
}
else
{ //平年2月
Console.WriteLine("输入天:");
day = Convert.ToInt32(Console.ReadLine());
if (day >= && day <= )
{
//日期正确
Console.WriteLine("日期正确");
}
else
{
//日期错误
Console.WriteLine("日期错误");
} }
}
} else
{
//月份错误
Console.WriteLine("月份错误");
} }
else
{
//年份错误
Console.WriteLine("年份错误");
}
}
3.axx+bx+c==0(a!=0)。输入a,b,c给这个一元二次方程,显示根的个数?
static void Main(string[] args)
{
Console.WriteLine("一元二次方程a*x*x+b*x+c=0,(a!=0)请输入a,b,c的值");
int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
int c = Convert.ToInt32(Console.ReadLine());
double de = b * b - * a * c; ; if (de>)
{
Console.WriteLine("这个方程有两个不等实根");
}
else if (de==)
{
Console.WriteLine("这个方程有两个相同的实根");
}
else
{
Console.WriteLine("这个方程没有实数根");
}
}
4.男士身高与体重的关系是:身高-100=体重; 女士:身高-110=体重。(自己试着做)
上下浮动3公斤属正常。
输入性别,身高,体重,输出:正常?偏胖?偏瘦?
static void Main4444(string[] args)
{ //男士身高与体重的关系是:身高-100=体重; 女士:身高-110=体重。(自己试着做)
//上下浮动3公斤属正常。
//输入性别,身高,体重,输出:正常?偏胖?偏瘦?
string sex;
int shengao, tizhong; //输入
Console.WriteLine("性别:");
sex = Console.ReadLine();
Console.WriteLine("身高:");
shengao = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("体重:");
tizhong = Convert.ToInt32(Console.ReadLine()); //运算输出 if (sex == "男")
{
int nbz = shengao - ;
if (nbz - tizhong >= - && nbz - tizhong <= )
{
Console.WriteLine("体重正常,继续保持");
}
else if (nbz - tizhong < -)
{
Console.WriteLine("偏胖,注意饮食,加强锻炼");
}
else
{
Console.WriteLine("偏瘦,注意营养");
}
}
else if (sex == "女")
{
int vbz = shengao - ;
if (vbz - tizhong >= - && vbz - tizhong <= )
{
Console.WriteLine("体重正常,继续保持");
}
else if (vbz - tizhong < -)
{
Console.WriteLine("偏胖,注意饮食,加强锻炼");
}
else
{
Console.WriteLine("偏瘦,注意营养");
}
}
else
{
Console.WriteLine("输入错误");
}
}
5.做一个跟计算机猜拳的小游戏。0-剪刀,1-石头,2-布
要求输出0,1,2,计算机生成随机数,与人类输入的相比较判断谁胜了。
计算机生成随机数:
Random rand = new Random();
int c = rand.Next(3);
static void Main(string[] args)
{
// 做一个跟计算机猜拳的小游戏。0-剪刀,1-石头,2-布
//要求输出0,1,2,计算机生成随机数,与人类输入的相比较判断谁胜了。
//计算机出拳
Console.WriteLine("0-剪刀,1-石头,2-布");
Random r = new Random();
int c = r.Next();
//人出拳
Console.WriteLine("请出拳:");
int ren = Convert.ToInt32(Console.ReadLine());
//比较
if (c==)
{
if (ren==)
{
Console.WriteLine("电脑出" + c);
Console.WriteLine("平局"); }
else if (ren==)
{
Console.WriteLine("电脑出" + c);
Console.WriteLine("你赢了!");
}
else if (ren==)
{
Console.WriteLine("电脑出" + c);
Console.WriteLine("电脑赢了,你输了");
}
else
{
Console.WriteLine("输入错误!请按规矩出牌");
} }
else if (c==)
{
if (ren == )
{
Console.WriteLine("电脑出" + c);
Console.WriteLine("电脑赢了");
}
else if (ren == )
{
Console.WriteLine("电脑出" + c);
Console.WriteLine("平局");
}
else if (ren == )
{
Console.WriteLine("电脑出" + c);
Console.WriteLine("你赢了");
}
else
{
Console.WriteLine("输入错误!请按规矩出牌");
}
}
else if (c==)
{
if (ren == )
{
Console.WriteLine("电脑出" + c);
Console.WriteLine("你赢了");
}
else if (ren == )
{
Console.WriteLine("电脑出" + c);
Console.WriteLine("电脑赢了,你输了");
}
else if (ren == )
{
Console.WriteLine("电脑出" + c);
Console.WriteLine("平局");
}
else
{
Console.WriteLine("输入错误!请按规矩出牌");
}
} }
6.做一个算缘分的小游戏:
输入男方姓名,女方姓名,输出缘分指数,给出建议。
static void Main(string[] args)
{
Console.Write("男方姓名:");
string nan = Console.ReadLine();
Console.Write("女方姓名:");
string nv = Console.ReadLine(); Random rand = new Random();
int n = rand.Next();
n++; string jianYi = "";
if (n > && n < )
{
jianYi = "分手吧";
}
else if (n >= && n < )
{
jianYi = "一起努力";
}
else if (n >= && n <= )
{
jianYi = "幸福一对";
}
else
{
jianYi = "鸳鸯配";
} Console.WriteLine("{0}和{1}的缘分指数是:{2}。建议:{3}", nan, nv, n,jianYi);
}
7.判断输入的两位数是否与7相关
static void Main(string[] args)
{
//判断输入的两位数是否与7相关 Console.WriteLine("请输入一个两位数:");
int a = Convert.ToInt32(Console.ReadLine()); if (a%==||a%==7 || a / 10 == 7)
{
Console.WriteLine("这个数与7相关。");
}
else
{
Console.WriteLine("这个数与7无关。");
} }
8.你能跑过豹子吗?
static void Main(string[] args)
{
//你能跑过豹子吗? Console.WriteLine("你能跑过豹子吗?(Y/N)");
string s = Console.ReadLine(); if (s=="y"||s=="Y")
{
Console.WriteLine("你比禽兽还禽兽!");
}
else
{
Console.WriteLine("你禽兽不如。");
}
}
9.判断输入的年份是闰年,还是平年
static void Main(string[] args)
{
//判断输入的年份是闰年,还是平年 Console.Write("请输入年份:");
int year = Convert.ToInt32(Console.ReadLine());
//1.被400整除; 2.被4整除,但不能被100整除
if (year%==||(year%!=&&year%==))
{
Console.WriteLine("是闰年");
}
else
{
Console.WriteLine("是平年");
}
}
10.输入三个数,三个数中找出最大的来
static void Main(string[] args)
{
//输入三个数,三个数中找出最大的来
Console.WriteLine("请输入三个数:");
int a, b, c;
int max = ;
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
c = Convert.ToInt32(Console.ReadLine()); if (a>b)
{
max = a;
}
else
{
max = b;
}
if (max<c)
{
max = c;
}
Console.WriteLine("最大数是"+max);
}
11.输入年龄,判断是童年,少年,青年,中年,老年。
static void Main(string[] args)
{
//输入年龄,判断是童年,少年,青年,中年,老年。
Console.WriteLine("请输入年龄:");
int age = Convert.ToInt32(Console.ReadLine());
if (age>&&age<=)
{
Console.WriteLine("童年");
}
else if (age>&&age<)
{
Console.WriteLine("少年");
}
else if (age>=&&age<=)
{
Console.WriteLine("青年"); }
else if (age>&&age<)
{
Console.WriteLine("中年");
}
else if (age>=&&age<)
{
Console.WriteLine("老年");
}
else
{
Console.WriteLine("输入错误");
}
}
if条件语句的更多相关文章
- Python —条件语句
条件语句 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语句的执行过程: Python程序语言指定任何非0和非空(null ...
- php简写表达式,&& or || 缩写条件语句
有时候学的多了, 好多小细节 都忘了 ,比如 简单的表达式, 三元表达式 ?:; $aa or $bb 表达式 等等! 写一些简单的表达式,备忘! php用&&和||缩写条件语句 ...
- java中的条件语句(if、if...else、多重if、嵌套if)
Java条件语句之 if 生活中,我们经常需要先做判断,然后才决定是否要做某件事情.例如,如果考试成绩大于 90 分,则奖励一个 IPHONE 5S .对于这种"需要先判断条件,条件满足后才 ...
- javascript语句——条件语句、循环语句和跳转语句
× 目录 [1]条件语句 [2]循环语句 [3]跳转语句 前面的话 默认情况下,javascript解释器依照语句的编写顺序依次执行.而javascript中的很多语句可以改变语句的默认执行顺序.本文 ...
- go语言条件语句 if else
示例: if a < 5 { return 0 } else { return 1 } 关于条件语句,需要注意以下几点: 条件语句不需要使用括号将条件包含起来(): 无论语句体内有几条语 ...
- Todd's Matlab讲义第4讲:控制误差和条件语句
误差和残量 数值求解方程\(f(x)=0\)的根,有多种方法测算结果的近似程度.最直接的方法是计算误差.第\(n\)步迭代结果与真值\(x^\*\)的差即为第\(n\)步迭代的误差: \begin{e ...
- 5-python学习——条件语句
5-python学习——条件语句 5-python学习——条件语句 条件语句if else形式 if else条件语句说明 测试一下 编程语言一般都由这么几个部分组成 变量 条件分支语句 循环语句 函 ...
- Python基础6- 流程控制之if条件语句
Python条件语句是通过判断一条或多条条件语句的执行结果来决定执行哪条代码块的.Python 中if 语句用于控制程序的执行,基本形式为:if 判断条件: 执行语句……else: 执行语句…… #c ...
- Jade之条件语句
条件语句 jade支持js中的if/elseif/else语法. jade: - var user = { description: 'foo bar baz' } - var authorised ...
- 不可或缺 Windows Native (3) - C 语言: 运算符,表达式,条件语句,循环语句,转向语句,空语句等
[源码下载] 不可或缺 Windows Native (3) - C 语言: 运算符,表达式,条件语句,循环语句,转向语句,空语句等 作者:webabcd 介绍不可或缺 Windows Native ...
随机推荐
- ABAP基本数据类型、通用类型
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- ABAP RFC远程调用
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- [资源] Resources on Self-Driving Car
Talk NVIDIA at CES 2016 - Self Driving Cars and Deep Learning GPUs Projects Autoware Open-source sof ...
- 编写spring配置文件时,不能出现帮助信息
由于spring的schema文件位于网络上,如果机器不能连接到网络,那么在编写配置信息时候就无法出现提示信息,解决方法有两种: 1.让机器上网,eclipse会自动从网络上下载schema文件并缓存 ...
- 2013 Multi-University Training Contest 1
HDU-4605 Magic Ball Game 题意:给定一颗以1为根的数,每个节点要么有两个孩子节点,要么没有孩子,每个节点有一个重量,现在从节点1往下放置一个小球,根据小球和节点的重量的不同球落 ...
- poj2079Triangle(N点中三点组成三角形面积最大)
链接 根据旋转卡壳的思想,找到当前边的最远点. 确定i,j找到最远的k使 cross(i,j,k)最大,那么i,j+1时只需从k+1开始找即可 . #include <iostream> ...
- mysql 求时间段平均值
考虑下面的需求,在一段时间内,间隔一段时间,取一个平均值,把所有的平均值取出来,怎么办?思路:在存储过程中,拼接sql语句.根据起始时间和结束时间,while循环每次加一段时间.DROP PROCED ...
- system v和posix的共享内存对比 & 共享内存位置
参考 http://www.startos.com/linux/tips/2011012822078.html 1)Linux和所有的UNIX操作系统都允许通过共享内存在应用程序之间共享存储空间. 2 ...
- 两个EXCEL文件对比去重
两个EXCEL文件,A里面有10000条记录,B里面有4000条记录,A的记录包含了B里面所有的记录现在如何能把A里面没有包含B的6000条记录筛选出来? 那你要把两同时打开,比如book1 book ...
- about opencl
Platform:LG G3,Adreno 330 1. 8M(3264x2448) memmap方式读入时间24ms,读出时间12ms,时间与内存大小基本成线性关系.使用memmap 与 memco ...