C#try-catch用法及水仙花数,九九乘法表

1.try-catch的使用

C#中异常捕获主要利用try-catch完成

Try //检测异常

{

   有可能出现错误的代码;

   代码;//如果上一行代码出错,则直接跳入catch中,本行代码就不会执行了。如果程序执行到本行代码,则上一行代码没有出错。

}

Catch

{

出错后的处理;(报错信息)

}

 

程序执行过程:

如果try中的代码没有出错,则程序正常运行try中的内容后,不会执行catch中的内容;

如果try中的代码一旦出错,程序立即跳入catch中,去执行代码,那么try中的出错代码后面的代码不再执行了

一个小例子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace try_catch
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入张三的分数?");
            try
            {
                int score = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("in try");//如果用户输入的是数字,则正常运行
            }
            catch
            {
               Console.WriteLine("in catch");//如果用户输入的不是数字则进入catch中,catch后的代码就不再执行了
            }
            Console.WriteLine("over");

            Console.WriteLine("请输入您的年龄?");
            try
            {
                int age = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("您刚刚输入的年龄为:" + age);
                //程序走到这里说明用户确实输入了一个数字
            }
            catch
            {
                Console.WriteLine("让你输入年龄,你看看你输入的是什么!!");
                //程序走到这里说明用户输入的不是一个数字,弹出报错信息,后面的代码不再执行
            }
            Console.ReadKey();
        }
    }
}


注意:异常处理方法二

处理利用try-catch方式来处理异常外,通常也可以使用throw new Exception();来处理异常

   

           Console.WriteLine("请输入张三的分数?");
            int score = Convert.ToInt32(Console.ReadLine());
            if (score < 0)
            {
                //throw new Exception("分数不能为负数");
                Exception ex = new Exception("分数不能为负数");
                throw ex;
            }
            else
            {
                Console.WriteLine("请重新输入");
            }
            Console.WriteLine(score);


2.水仙花数

水仙花数是一个三位数,设cba

a*a*a+b*b*b+c*c*c=cba

153=1*1*1+5*5*5+3*3*3

水仙花数是指一个 位数 ( n≥3 ),它的每个位上的数字的 次幂之和等于它本身。

例如:

1^3 + 5^3 + 3^3 = 153

水仙花数只是自幂数的一种

,严格来说三位数的3次幂数才成为水仙花数。 附:其他位数的自幂数名字 一位自幂数:独身数    两位自幂数:没有      三位自幂数:水仙花数

小例子:求100-999之间的水仙花数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _02水仙花数
{
    class Program
    {
        static void Main(string[] args)
        {
            //水仙花数
           // 三位数的3次幂数才成为水仙花数
            for (int i = 100; i <= 999; i++)//循环 从100到999 查找100-999之间的水仙花数
            {
                int ge = i % 10;
                int shi = i % 100 / 10;
                int bai = i / 100;
                if (i == ge * ge * ge + shi * shi * shi + bai * bai * bai)//1^3 + 5^3 + 3^3 = 153
                {
                    Console.WriteLine(i);
                }
            }
                Console.ReadKey();
        }
        
    }
}

3.九九乘法表实现(循环嵌套

斜三角九九乘法表的实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 九九乘法表
{
    class Program
    {
        static void Main(string[] args)
        {
            //实现斜三角的九九乘法表
            for (int i = 1; i <= 9; i++)//控制行数由1至9的变化
            {
                for (int j = 1; j <= i; j++)//控制列数(行数增加时列数也随着增加)形成9行9列的效果
                {
                    Console.Write("{0}*{1}={2} \t", j, i, i * j);//注意:占位符“{2}”与“\t”之间是有空格的,目的是保持距离。
                }
                Console.WriteLine();//输出空白行
            }
            Console.ReadKey();
         }
    }
}


注意:

\n 表示换行

\b  backspace 表示退格

\t  tab键  由多个空格组成的一个字符,具有行与行之间的对齐功能

\\ 表示一个\

\r 回车



实现长方形九九乘法表

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 九九乘法表
{
    class Program
    {
        static void Main(string[] args)
        {
            //实现长方形的九九乘法表
            for (int i = 1; i <= 9; i++)
            {
                //外层循环控制行数,即i代表行号
                for (int j = 1; j <= 9; j++)
                {
                    //第i行的第j个式子
                    //{2:00} 占位符必须是两位,不足两位补0
                    Console.Write("{0}*{1}={2}\t", i, j, i * j);
                }
                Console.WriteLine();//输出空白行
            }
            Console.ReadKey();
        }
    }
}


---------------------- ASP.Net+Unity开发.Net培训、期待与您交流! ----------------------

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值