学习软件设计——C#练习(11)

 1、除零异常
DivideByZeroException

using System;
using System.Collections.Generic;

public class MyClass
{
	public static void Main()
	{
		Console.WriteLine("请输被除数:");
		int a=int.Parse(Console.ReadLine());
		Console.WriteLine("请输除数:");
		int b=int.Parse(Console.ReadLine());
		try
		{
				int z = a / b;
		}
		catch (DivideByZeroException e)
		{
			Console.Write("除数不能为0哦,亲" + e.Message);
		}
		finally
		{
				Console.Write("无论是否有异常,finally都会出现!");
		}
		Console.ReadKey();
	}

}



2、数组越界

IndexOutOfRangeException

using System;
namespace ArrayException
{
 class Array
 {
  public void CalculateSum()
  {
   int sum = 0;
   int[] number = new int [5]{1,1,1,1,1};
   
   try
   {
    for (int i=1;i<=5;i++)
    {
     sum = sum + number[i];
    } 
    Console.WriteLine("The sum of the array is : {0} ",sum);
   }

   catch(IndexOutOfRangeException e)
   {
    Console.WriteLine("Index was outside the bounds of the array!");
   }
  }   
 } 
 class test
 {
  public static void Main(string[] args)
  {
   Array obj=new Array();
   obj.CalculateSum();
   Console.ReadLine();
  }
 }
}



3、溢出异常
OverFlowException

using System;

class stack
{
	public static void over1() 
	{
		try 
		{
			throw new StackOverflowException();
		} 
		catch (StackOverflowException e) 
		{
			Console.WriteLine(e.Message);
		}
	}
	
	public static void over2() 
	{
		try 
		{
			over(10);
		} 
		catch (StackOverflowException e) 
		{
			Console.WriteLine(e.Message);
		}
	}
	
	public static int over(int p) 
	{
		return 10 + over(p+10);
	}
	
	public static void Main(string[] args) 
	{
		over1();
		over2();
		//Console.WriteLine("异常的处理");
		Console.ReadKey();
	}
}



4、格式不正确
FormatException

using System;
using System.Collections.Generic;

public class ExceptionTest {

	public static void Main(String[] args) {
		String s = "cat miaomiao";
		try {
			int a=int.Parse(s);
		} catch (FormatException e) {
			Console.WriteLine("无法将该字符串转换为数字!");
			Console.ReadKey();
		}
	}
} 



5、finally练习
6、throw练习

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

public class TEApp
    {
        public static void ThrowException()
        {
            throw new Exception();
        }
        public static void Main()
        {
            try
            {
                ThrowException();
                Console.WriteLine("try");
            }
            catch (Exception e)
            {
                Console.WriteLine("catch");
            }
            finally
            {
                Console.WriteLine("finally");
            }
			Console.ReadLine();
        }
    }


7、自定义异常

using System;

public class EmailException:ApplicationException
{
 public EmailException(string e): base(e){}
}

class Program
{
 static void Main()
 {
  Console.WriteLine("请输入Email地址");
  string email = Console.ReadLine();
  string[]substrings = email.Split('@');
  
  try
  {
   if (substrings.Length != 2)
   {
    throw new EmailException("email地址错误");
   }
   else
   {
    Console.WriteLine("输入正确");
   }
  }
  catch (EmailException ex)
  {
   Console.WriteLine(ex.Message);
  }
  
  Console.ReadLine();
 }
}


 

 

1. 声明两个变量:int n1 = 10, n2 = 20;要求将两个变量交换,最后出n1为20,n2为10。扩展(*):不使用第三个变量如何交换? 2. 用方法来实现:将上题封装一个方法来做,方法有两个参数分别为num1,num2,将num1与num2交换。提示:方法有两个参数n1,n2,在方法中将n1与n2进行交换,使用ref。(*) 3. 请用户一个字符串,计算字符串中的字符个数,并出。 4. 用方法来实现:计算两个数的最大值。思考:方法的参数?返回值?扩展(*):计算任意多个数间的最大值(提示:使用可变参数,params)。 5. 用方法来实现:计算1-100之间的所有整数的和。 6. 用方法来实现:计算1-100之间的所有奇数的和。 7. 用方法来实现:判断一个给定的整数是否为“质数”。 8. 用方法来实现:计算1-100之间的所有质数(素数)的和。 9. 用方法来实现:有一个整数数组:{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 },找出其中最大值,并出。不能调用数组的Max()方法。 10. 用方法来实现:有一个字符串数组:{ "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" },请出最长的字符串。 11. 用方法来实现:请计算出一个整型数组的平均值。{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }。要求:计算结果如果有小数,则显示小数点后两位(四舍五入)。Math.Round() 12. 请通过冒泡排序法对整数数组{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }实现升序排序。 13. 有如下字符串:【"患者:“大夫,我咳嗽得很重。” 大夫:“你多大年记?” 患者:“七十五岁。” 大夫:“二十岁咳嗽吗”患者:“不咳嗽。” 大夫:“四十岁时咳嗽吗?” 患者:“也不咳嗽。” 大夫:“那现在不咳嗽,还要等到什么时咳嗽?”"】。需求:①请统计出该字符中“咳嗽”二字的出现次数,以及每次“咳嗽”出现的索引位置。②扩展(*):统计出每个字符的出现次数。 14. 将字符串" hello world,你 好 世界 ! "两端空格去掉,并且将其中的所有其他空格都替换成一个空格,出结果为:"hello world,你 好 世界 !"。 15. 制作一个控制台小程序。要求:用户可以在控制台录入每个学生的姓名,当用户入quit(不区分大小写)时,程序停止接受用户入,并且显示出用户入的学生的个数,以及每个学生的姓名。效果如图:
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值