语句是指程序命令,都是按照顺序执行的。

语句又分为:

顺序语句:从上到下按顺序执行,挨个执行一遍。

分支语句:选择性执行语句,有的可能会执行,有的可能不执行。满足条件执行。

循环语句:

一、分支语句 if...else...

(1)格式

if(条件)

{满足此条件要执行的代码}

else if(条件)

{满足此条件要执行的代码}

else    //否则

{不满足上述条件的要执行的代码}

(2)、

必须以if开头,可以是else if结束,也可以是else结束,也可以直接结束。
if (bool类型(比较表达式))
{
如果上面的条件成立,那么会执行这里面的代码
}
else if (bool类型 (比较表达式))
{
走这里的代码
}
else //只要上面条件都不成立,那么必走else里的代码
{

}

(3)练习题

1、“请输入年份:”
判断是否是闰年,“xxxx年是闰年”,“xxxx年不是闰年”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace 练习题1
{
class Program
{
static void Main(string[] args)
{
Console.Write("请输入年份:");
int a = Convert.ToInt32(Console.ReadLine());
if (a % == && a % != )
{
Console.WriteLine(a+"年是闰年");
}
else if (a % == )
{
Console.WriteLine(a + "年是闰年");
}
else
{
Console.WriteLine(a+"年不是闰年");
} Console.ReadLine();
}
}
}

2、“请输入您的分数:”
小于0,大于100,“输入的分数有误!”
大于0,小于10,“不及格!学渣!”
小于60,“不及格!继续努力!”
大于等于60,“恭喜你!及格了!”
大于等于90,“学霸!很厉害!”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace 练习题2
{
class Program
{
static void Main(string[] args)
{
Console.Write("请输入您的分数:");
double a = Convert.ToDouble(Console.ReadLine()); //小于0,大于100,“输入的分数有误!”
//大于0,小于10,“不及格!学渣!”
//小于60,“不及格!继续努力!”
//大于等于60,“恭喜你!及格了!”
//大于等于90,“学霸!很厉害!” if (a < && a > )
{
Console.WriteLine("输入的分数有误!");
}
else if (a > && a < )
{
Console.WriteLine("不及格!学渣!");
} else if (a < )
{
Console.WriteLine("不及格,继续努力!");
}
else if (a >= )
{
Console.WriteLine("学霸!很厉害!");
} else if (a >= )
{
Console.WriteLine("恭喜你,及格了!"); } Console.ReadLine();
}
}
}

3、猜拳
“请输入您的手势(石头/剪子/布):”
“用户赢了” “电脑赢了” “平局”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace 练习题3
{
class Program
{
static void Main(string[] args)
{ //让用户输入手势
Console.Write("请输入您的手势:");
string user = Console.ReadLine(); //电脑生成手势
//0石头 1剪刀 2布
Random r =new Random();
int com =r.Next(,); //手势对比
int user1;
if(user=="石头")
{
user1 = ;
}
else if (user == "剪刀")
{
user1 = ;
}
else
{
user1 = ;
} //对比输赢 if (user1 - com == - || user1 - com == )
{
Console.WriteLine("您赢了!");
}
else if (user1 - com == - || user1 - com == )
{
Console.WriteLine("电脑赢了!");
}
else
{
Console.WriteLine("平局!");
} Console.ReadLine();
}
}
}

4、人工智能对话
如果说的是同一句话,不一定要回复同一句

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace 练习题4
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hi 我是Siri,请问您需要帮助吗?");
string a=Console.ReadLine(); Random r = new Random();
int com = r.Next(, ); if (com==)
{
Console.WriteLine("凡事靠自己,不要总想着靠别人");
}
else if (com== )
{Console.WriteLine("需要帮助我也没啥好帮你的!");
}
else if (com == )
{
Console.WriteLine("不需要帮助你找我干嘛!!!");
}
else if (com == )
{
Console.WriteLine("乱写什么!问你需要还是不需要!!!");
} Console.ReadLine();
}
}
}

二、分支嵌套、变量的作用域

1、分支嵌套就是在if或者else if 或者 else 下嵌套if...else...语句

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace 分支嵌套
{
class Program
{
static void Main(string[] args)
{
int a = ; if (a < || a > )//分数有误走这里
{
Console.WriteLine("分数输入有误!");
}
else //分数正确走这里
{
Console.WriteLine("分数输入正确!");
if (a >= )
{
Console.WriteLine("及格了!"); if (a >= )
{
Console.WriteLine("学霸!");
}
}
else
{
Console.WriteLine("不及格!"); if (a < )
{
Console.WriteLine("学渣!");
}
} } }
}
}

2、变量的作用域

“儿子可以用爹的所有东西”   “爹不能用儿子的东西”

最新文章

  1. 基于Visual Studio Code搭建Golang开发调试环境【非转载】
  2. Backbone源码学习之extend
  3. Android Studio项目目录结构介绍——android菜鸟成长之路
  4. Spring的Lifecycle
  5. Discuz 3.X 门户文章插入图片自动添加 alt 标签
  6. Oracle 6 - 锁
  7. 关于Xcode的Other Linker Flags
  8. 1001. 害死人不偿命的(3n+1)猜想
  9. Android开发(25)--framebyframe帧动画并实现启动界面到主界面的跳转
  10. JAVA多态问题总结(课堂总结)
  11. AIO75产品特征与优势
  12. Unity Shader笔记
  13. word文档内容如何防止被复制
  14. 100-days: twelve
  15. vue 使用mint-ui实现上拉加载和下拉刷新
  16. 我终于激活Windows Server2008 R2了!!
  17. 用c#监控网络状态
  18. http头文件User-Agent详解【转载】
  19. electron安装到第一个实例
  20. Courses HDU - 1083 (二分匹配模板题)

热门文章

  1. contextMenu,右键菜单
  2. java_web学习(12)JDBC
  3. globalToLocal的坐标变换
  4. 在ASP.NET MVC中使用 Bootstrap table插件
  5. 关于aop:pointcut的expression配制说明及JoinPoint
  6. 分析器错误消息: 无法识别的属性“targetFramework”。
  7. Bootstrap入门(十一)组件5:输入框组
  8. SoapUI:使用Excel进行参数化
  9. OC基础了解篇
  10. Spark RDD算子介绍