码迷,mamicode.com
首页 > 其他好文 > 详细

if 语句

时间:2016-03-07 22:33:25      阅读:307      评论:0      收藏:0      [点我收藏+]

标签:

if 语句的分类

if (){}  按顺序执行

if (){} else {}   二选一     若if成立,则不去走else,若if不成立,则一定会走else。

if(){}else if(){} else if (){}  else {}      多选一    若if 成立,那其他的所有都不去看,若if不成立,去查看下一个else if 成立不成立,若成立,剩下的不去看。

    if 的嵌套
if ()
 {
   if()
   {

   }
   else
   {

   }

 }

 

练习:输入三个数,xyz,最终以从小到大的方式输出,利用if嵌套

 Console.Write("请输入x=");
            int x = int.Parse(Console.ReadLine());
            Console.Write("请输入y=");
            int y = int.Parse(Console.ReadLine());
            Console.Write("请输入z=");
            int z = int.Parse(Console.ReadLine());
            if (x < y && x < z) //判断x是不是最下的数
            {
                Console.WriteLine("x="+x);  //若是,先输出x
                if (y < z)       //判断剩余两个数的大小
                {
                    Console.WriteLine("y="+y);   //根据大小顺序输出
                    Console.WriteLine("z="+z);
                }
                else
                {
                    Console.WriteLine("z"+z);     //根据大小顺序输出
                    Console.WriteLine("y="+y);
                }

            }
            if (y < z && y < x)
            {
                Console.WriteLine("y="+y);
                if (z < x)
                {
                    Console.WriteLine("z="+z);
                    Console.WriteLine("x="+x);
                }
                else
                {
                    Console.WriteLine("x="+x);
                    Console.WriteLine("z="+z);
                }
            }
            else
            {
                Console.WriteLine("z="+z);
                if (x < y)
                {
                    Console.WriteLine("x="+x);
                    Console.WriteLine("y="+y);
                }
                else
                {
                    Console.WriteLine("y="+y);
                    Console.WriteLine("x="+x);
                }
            }

 

 

 

技术分享

将两个数调换:

int a = 2;
int b = 5;

int zhong=a;
a = b;
b = zhong;

改变 :要求必须以 x  y   z   的形式输出,用上面的调换方法

 1  Console.Write("请输入x=");
 2             int x = int.Parse(Console.ReadLine());
 3             Console.Write("请输入y=");
 4             int y = int.Parse(Console.ReadLine());
 5             Console.Write("请输入z=");
 6             int z = int.Parse(Console.ReadLine());
 7             int zhong;
 8             if (x < y && x < z)
 9             {
10                 if (y < z)
11                 {
12                 }
13                 else
14                 {
15                     zhong = y;   //将y与z调换
16                     y = z;
17                     z = zhong;
18                 }
19             }
20             if (y < z && y < x)
21             {
22                 zhong = y;     //将x与y调换
23                 y = x;
24                 x = zhong;
25                 if (z < y)
26                 {
27                     zhong = y;    //将y与z调换
28                     y = z;
29                     z = zhong;
30                 }
31                 else
32                 {
33 
34                 }
35             }
36             else
37             {
38                 zhong = z;       //将 x与z调换
39                 z = x;
40                 x = zhong;
41                 if (z < y)
42                 {
43                     zhong = y;      //将y与z调换
44                     y = z;
45                     z = zhong;
46 
47                 }
48                 else
49                 {
50 
51                 }
52             }
53             Console.WriteLine("x="+x);
54             Console.WriteLine("y="+y);
55             Console.WriteLine("z="+z);

技术分享

相亲过程

女方问:你有房吗?  

若有→女方:结婚吧;若没有→女方:你有钱吗?

若有→女方:买了房子再结婚;若没有→女方:你有能力吗?

若有→女方:赚了钱买了房子再结婚 ; 若没有→女方:拜拜。

 1  Console.Write("你有房子吗?");
 2             string m = Console.ReadLine();
 3             if (m == "")
 4             {
 5                 Console.WriteLine("结婚吧");
 6             }
 7             if (m == "没有")
 8             {
 9                 Console.WriteLine("你有钱吗?");
10                 m = Console.ReadLine();
11                 if (m == "")
12                 {
13                     Console.WriteLine("先买房子再结婚");
14                 }
15                 if (m == "没有")
16                 {
17                     Console.WriteLine("你有能力吗?");
18                     m = Console.ReadLine();
19                     if (m == "有")
20                     {
21                         Console.WriteLine("先赚钱再买房子再结婚");
22                     }
23                     if (m == "没有")
24                     {
25                         Console.WriteLine("拜拜");
26                     }
27                 }
28             }

 

技术分享

输入考生成绩

 1   Console.Write("请输入考生姓名:");
 2             string name = Console.ReadLine();
 3             Console.Write("请输入考试成绩:");
 4             double i = double.Parse(Console.ReadLine());
 5             if (i >= 0 && i <= 100)  //成绩必须在0到100之间
 6             {
 7                 if (i == 100) //100分的时候
 8                 {
 9                     Console.WriteLine("恭喜你" + name + ",满分通过");
10                 }
11                 else if (i >= 80) // 80到100的时候
12                 {
13                     Console.WriteLine(name + ",你很优秀,继续保持");
14                 }
15                 else if (i >= 60)  // 60到80的时候
16                 {
17                     Console.WriteLine(name + "成绩良好");
18                 }
19                 else if (i >= 50)  //50到60的时候
20                 {
21                     Console.WriteLine(name + "就差一点点,下次一定要至少及格");
22                 }
23                 else  // 50分以下的时候
24                 {
25                     Console.WriteLine(name + "你是笨蛋吗");
26                 }
27             }
28             else    // 超出100或负数的时候
29             {
30                 Console.WriteLine("您的输入有误");
31             }

技术分享

输入一段分段函数

            Console.Write("输入x=");
            double x = double.Parse(Console.ReadLine());
            double y;
            if (x < 1)
            {
                y = x;
            }
            else if (x < 10)
            {
                y = 2 * x - 1;
            }
            else
            {
                y = 3 * x - 11;
            }
            Console.WriteLine("y=" + y);

求解一元二次方程a*x^2+b*x+c=0的根

 1  Console.WriteLine("求解a*x^2+b*x+c=0的根");
 2             Console.Write("请输入a=");
 3             double a = double.Parse(Console.ReadLine());
 4             Console.Write("请输入b=");
 5             double b = double.Parse(Console.ReadLine());
 6             Console.Write("请输入c=");
 7             double c = double.Parse(Console.ReadLine());
 8             if (a != 0)
 9             {
10                 double s = b * b - 4 * a * c;
11                 if (s >= 0)
12                 {
13                     double x1 = (-b + Math.Sqrt(s)) / (2 * a);
14                     double x2 = (-b - Math.Sqrt(s)) / (2 * a);
15                     if (s > 0)
16                     {
17                         Console.WriteLine("该方程有两个不同实数根");
18                         Console.WriteLine("方程的根为x1=" + x1 + "  x2=" + x2);
19                     }
20                     else
21                     {
22                         Console.WriteLine("方程有两个相同的实数根");
23                         Console.WriteLine("方程的根为x1=x2=" + x1);
24                     }
25                 }
26                 else
27                 {
28                     Console.WriteLine("该方程没有实数根");
29                 }
30 
31             }
32             else
33             {
34                 Console.WriteLine("该函数不是一元二次函数");
35             }

 

if 语句

标签:

原文地址:http://www.cnblogs.com/zk0533/p/5250901.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!