代码改变世界

分支结构之IF语句

  精诚所至 金石为开  阅读(287)  评论(0编辑  收藏  举报

本示例演示IF语句的使用,比较两个数值的大小,并输出结果。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System;
namespace a
{
    class Program
    {
        public static void Main(string[] args)
        {
            string comparison;
            Console.WriteLine("Enter a number:");
            double var1=Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Enter another number:");
            double var2=Convert.ToDouble(Console.ReadLine());
            if(var1<var2)
                comparison="less than";
            else
            {
                if(var1==var2)
                    comparison="equal to";
                else
                    comparison="greater than";
            }
            Console.WriteLine("The first number is {0} the second number.",comparison);
            Console.ReadKey();
        }
    }
}
努力加载评论中...
点击右上角即可分享
微信分享提示