c#——if语句

if 语句

(一)if语句的格式(如下图)

例1:简单的if语句

namespace 布尔表达式 { class Program { static void Main(string[] args) { Console.WriteLine("请输入年龄"); string s1 = Console.ReadLine(); int age = Convert.ToInt32(s1); if (age>20) { Console.WriteLine("成年人!"); //age<=20不执行 } Console.ReadKey(); } } }

例2: if…… else语句

namespace 布尔表达式 { class Program { static void Main(string[] args) { Console.WriteLine("请输入年龄"); string s1 = Console.ReadLine(); int age = Convert.ToInt32(s1); if (age > 20) { Console.WriteLine("成年人!"); } else //否则 { Console.WriteLine("未成年人!"); } Console.ReadKey(); } } }

例3:if ……else if ……else 语句

namespace 布尔表达式 { class Program { static void Main(string[] args) { Console.WriteLine("请输入年龄"); string s1 = Console.ReadLine(); int age = Convert.ToInt32(s1); if (age > 20) { Console.WriteLine("成年人!"); } else if (age > 10) { Console.WriteLine("青年人!"); } else { Console.WriteLine("婴儿"); } Console.ReadKey(); } } }

(二)易错的地方


注意1:if ()后面不要加; ,如果加了; 表示if语已经结束,后面的{}的if已经没有关系(如例4)

例4

namespace 布尔表达式 { class Program { static void Main(string[] args) { Console.WriteLine("请输入年龄"); string s1 = Console.ReadLine(); int age = Convert.ToInt32(s1); if (age > 20) ; //错误: ;表示if语已经结束,后面的{}的if已经没有关系 { Console.WriteLine("成年人!"); } Console.ReadKey(); } } }

例4的执行结果如下:

错误:当输入小于等于20的数,输出的却是成年人。

改正:参看上面的例1或下面的例5

例5

namespace 布尔表达式 { class Program { static void Main(string[] args) { Console.WriteLine("请输入年龄"); string s1 = Console.ReadLine(); int age = Convert.ToInt32(s1); /* if (age > 20) ; //错误的代码,加了“;”。 //;表示if语已经结束,后面的{}的if已经没有关系 */ if (age > 20) //正确的代码,注意不要加“;”。 { Console.WriteLine("成年人!"); } Console.ReadKey(); } } }

注意2:if 语句哪怕只有一行语句,最好也要加大括号{} 。

例5:

namespace 布尔表达式 { class Program { static void Main(string[] args) { Console.WriteLine("请输入年龄"); string s1 = Console.ReadLine(); int age = Convert.ToInt32(s1); if (age > 18) //if后没加大括号{}. Console.WriteLine("成年人!"); Console.WriteLine("恭喜您已成年"); Console.ReadKey(); } } }

例5执行结果如下:

代码改正(如下例6)

例6

namespace 布尔表达式 { class Program { static void Main(string[] args) { Console.WriteLine("请输入年龄"); string s1 = Console.ReadLine(); int age = Convert.ToInt32(s1); if (age > 18) //哪怕if语句中只有一行代码,也要加大括号{}。 { Console.WriteLine("成年人!"); Console.WriteLine("恭喜您已成年"); } Console.ReadKey(); } } }


(三)精典示例

练习1:提示用户输入密码,如果密码是“888888”则提示正确,否则提示错误。

错误代码如下:

namespace 布尔表达式 { class Program { static void Main(string[] args) { //提示用户输入密码,如果密码是“888888”则提示正确,否则提示错误。 Console.WriteLine("请输入密码"); string s1 = Console.ReadLine(); int pwd = Convert.ToInt32(s1); //问题1,用户输入非数字的时候失败 //问题2,用户输入过长的数字也报错。 if (pwd==888888) { Console.WriteLine("密码正确!"); } else { Console.WriteLine("密码不正确!"); } Console.ReadKey(); } } }

修改后的正确代码如下:

namespace 布尔表达式 { class Program { static void Main(string[] args) { //提示用户输入密码,如果密码是“888888”则提示正确,否则提示错误。 Console.WriteLine("请输入密码:"); string s1 = Console.ReadLine(); //int pwd = Convert.ToInt32(s1); //删除掉上行代码,直接判断输入的变量s1是否与字符串"888888"相等? if (s1=="888888") //密码"888888",只是看起来像数字而已。 { Console.WriteLine("密码正确!"); } else { Console.WriteLine("密码不正确!"); } Console.ReadKey(); } } }

注意:"888888"只是看起来像数字而已,其实不是数字,而是字符串。

练习2:提示用户输入密码,如果密码是“888888”则提示正确,否则要求再输入一次,如果密码是“888888”则提示正确,否则提示错误。(此例讲述的主要是if语句的嵌套
代码如下:

namespace 布尔表达式 { class Program { static void Main(string[] args) { //提示用户输入密码,如果密码是“888888”则提示正确,否则要求再输入一次,如果密码是“888888”则提示正确,否则提示错误。 Console.WriteLine("请输入密码:"); string s1 = Console.ReadLine(); if (s1=="888888") //密码"888888",只是看起来像数字而已。 { Console.WriteLine("密码正确!"); } else { Console.WriteLine("密码错误,请再输入一次:"); s1 =Console.ReadLine(); //使用变量s1,而不是重新定义变量 if (s1 == "888888") { Console.WriteLine("密码正确!"); } else { Console.WriteLine("密码错误!"); } } Console.ReadKey(); } } }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值