c#——if语句

if 语句

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

例1:简单的if语句

[csharp] view plaincopyprint?
<span style="font-size:16px;">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();  
        }  
    }  
}</span>  

例2:  if…… else语句
[csharp] view plaincopyprint?
<span style="font-size:16px;">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();  
        }  
    }  
}  
</span>  

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

[csharp] view plaincopyprint?
<span style="font-size:16px;">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();  
        }  
    }  
}  
</span>  

(二)易错的地方


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

例4

[csharp] view plaincopyprint?
<span style="font-size:16px;">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();  
        }  
    }  
}</span>  

例4的执行结果如下:

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

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

例5

[csharp] view plaincopyprint?
<span style="font-size:16px;">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();  
        }  
    }  
}  
</span>  

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

例5:

[csharp] view plaincopyprint?
<span style="font-size:16px;">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();  
        }  
    }  
}</span>  

例5执行结果如下:

代码改正(如下例6)

例6

[csharp] view plaincopyprint?
<span style="font-size:16px;">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();  
        }  
    }  
}</span>  

(三)精典示例

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

错误代码如下:

[csharp] view plaincopyprint?
<span style="font-size:16px;">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();  
        }  
    }  
}  
</span>  

修改后的正确代码如下:

[csharp] view plaincopyprint?
<span style="font-size:16px;">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();  
        }  
    }  
}  
</span>  

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

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

[csharp] view plaincopyprint?
<span style="font-size:16px;">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();  
        }  
    }  
}  
</span>  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值