C#之咿呀学语(1)



                   最近小菜虫还是秉承实践出真理的方式学习C#,亲自把一段段代码敲出来,才有些感觉。

       下面是学习C#语句过程中敲的例子,有选择语句,迭代语句,跳转语句,异常处理语句。

       一个个都是在打包,下面开始从例子开始语法吧!

//if-else语句的用法
	bool  flag =false ;
            if (flag == true)
            {
                Console.WriteLine("The flag is true.");
            }
            else
            {
                Console.WriteLine("The flag is false.");
                
            }
       Console.Read(); 
//if-else多分支结构
	Console.WriteLine("输出结果为:");
            char c = 'D';
            if (Char.IsLetter(c))
            {
                if (Char.IsLower(c))
                {
                    Console.WriteLine("这是一个小写字母");
                }
                else
                {
                    Console.WriteLine("这是一个大写字母");
                }


//else if 阶梯语句的使用 
	Console.WriteLine("输出结果为:\n");
            char c = 'A';
            if(Char.IsUpper (c))
            {
                Console.WriteLine(c+"为大写字母");
            }
            else if(Char.IsLower(c))
            {
                Console.WriteLine(c+"为小写字母");
            }
            else if(Char.IsDigit (c))
            {
                Console.WriteLine("请输入有效字符");
            }
                Console.ReadLine ();          }
            else
            {
                Console.WriteLine("这不是字母");
            }
            Console.ReadLine();
//利用空case语句检查数字在什么范围内  
	Console.WriteLine("输出结果为:\n ");
            int n = 1;
            switch (n)
            {
                case 1:
                case 2:
                case 3:
                    Console.WriteLine("不能确定输入的是1,2,3");
                    break;
                default :
                    Console.WriteLine("不能确定输入值的范围");
                    break;             }
            Console.ReadLine();
//case标签内使用goto语句跳转到另一个case标签,判断用户输入的值,进而完成费用的计算。 
	Console.WriteLine("输出结果为:\n选对号码:1=小 2=大 3=很大");
            Console.Write("请输入你的选择:");
            string s = Console.ReadLine();
            int n = int.Parse(s);
            int cost = 0;
            switch (n)
            {
                case 1:
                    cost+=25;
                    break;
                case 2:
                    cost+=25;
                    goto case 1;
                case 3:
                    cost+=50;
                    goto case 1;
                default :
                    Console.WriteLine("请按要求输入信息");
                    break;            }            if (cost!= 0)
            {
                Console.WriteLine("应付费用为{0}",cost );
            }
            Console.Read();

//利用while语句循环输出5个数字
	Console.WriteLine("输出结果为:");
            int n = 1;
            while (n < 6)
            {
                Console.WriteLine("循环第{0}次",n);
                n++;
            }
            Console.Read();

//利用do-while语句循环的流程
	Console.WriteLine("输出结果为:");
            int n = 1;
            do
            {
                Console.WriteLine("循环{0}次", n);
                n++;
            }
            while (n < 0);
            Console.Read();

//for语句的使用
	for (int i = 0, j = 10; i <= j; i++, j--)
            {
                Console.WriteLine("{0},{1}",i,j);
            }
            Console.Read();

//利用foreach输出整数数组的内容
	Console.WriteLine("输出结果:");
            int[] fibarray = new int[] { 0, 1, 2, 3, 5 };
            foreach (int i in fibarray)
            {
                System.Console.WriteLine(i);            }
            Console.Read();
//break语句在计数达到4后终止循环
	Console.WriteLine("输出结果为:");
            for (int i = 1; i <= 10; i++)
            {
                if (i > 4)
                {
                    break;
                }
                Console.WriteLine(i);
            }
            Console.Read();
 














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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值