一、实验目的
(1)掌握C#中数组的使用
(2) 掌握Array类的属性和方法的使用
(3) 掌握ArrayList实现动态数组的编程方法
(4) 理解异常处理机制
二、实验内容
- Random类与数组练习
编写控制台程序,通过Random类产生10个10-50之间的随机整数并输出,后将它们按照从小到大和从大到小的顺序输出,如图2-1所示。
图2-1 运行效果图
说明:
(1)Random类提供了产生随机数的方法。产生随机数的方法必须由Random类创建的对象调用。Random类创建对象的格式为:
Random 随机对象名称=new Random();
如果要声明一个随机对象rn,则代码为:
Random rn = new Random();
对象名称.Next(整数1,整数2)
方法与属性格式 功 能 示 例
对象名称.Next(正整数) 产生0~正整数之间的随机整数 rn.Next(100)
对象名称.Next(整数1,整数2) 产生2个指定整数之间的随机整数
(2)对数组排序,可使用Array.Sort(数组名)实现数组元素升序排列,使用Array .Reverse(数组名)再将数组反转从而实现降序排列。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Experiment_2
{
internal class Program
{
static void Main(string[] args)
{
/*1. Random类与数组练习
编写控制台程序,通过Random类产生10个10 - 50之间的随机整数并输出,
后将它们按照从小到大 和 从大到小 的顺序输出
*/
Random random = new Random();//创建随机数对象
int[] nums=new int[10];
for(int i = 0; i < nums.Length; i++)
{
nums[i] = random.Next(10,51);//获取 10到50的数
}
Array.Sort(nums);//对数字进行排序 默认从小到大排序
Console.WriteLine("从小到大");
foreach(int i in nums)
{
Console.Write(i+" \t");//输出
}
Console.WriteLine();//换行
Console.WriteLine("从大到小");
for(int i = nums.Length-1; i >=0 ; i--)
{
Console.Write(nums[i] + " \t");//排序后反向输出
}
Console.WriteLine();//换行
Array.Reverse(nums);
Console.WriteLine("使用Reverse实现反向输出");
foreach (int i in nums)
{
Console.Write(i + " \t");//输出
}
Console.WriteLine();//换行
}
}
}
实验结果:
我们看到 随机生成了 21 23 27 32 34 35 36 40 41 然后就根据要求输出了
2. 编写控制台程序,从键盘输入2个数,输出二者的商,要求采用异常处理,若输入的不是数值数据,输出“应输入数值数据”;若输入的除数为0,输出“除数不能为0”。
3.
4. 图2-2 输入数据格式正确时运行效果图
图2-3 输入非数值型数据时运行效果图图2-4 输入除数为0时运行效果图
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Experiment_2
{
internal class Program2
{
/*编写控制台程序,从键盘输入2个数,输出二者的商,
* 要求采用异常处理,若输入的不是数值数据,
* 输出“应输入数值数据”;
* 若输入的除数为0,输出
* “除数不能为0”。
*
*/
static void Main(string[] args)
{
try
{
Console.WriteLine("从键盘输入第一个数");
int a = int.Parse(Console.ReadLine());
Console.WriteLine("从键盘输入第二个数");
int b = int.Parse(Console.ReadLine());
double c = a / b;//因为 c#中 double类型除以0有意义就不会有异常
c = (double)a / b;//运行到此说明除数不为0 这样就能输出小数了
Console.WriteLine("二者的商为{0}", c);
}
catch(DivideByZeroException e)//当试图用整数类型数据除以零时抛出。
{
Console.WriteLine("除数不能为0");//输出异常
}
catch(FormatException e)
{
Console.WriteLine("应输入数值数据");//输出异常
}
Console.WriteLine();
}
}
}
- 编写控制台程序,利用ArrayList进行数组操作,随机输入若干个数据(以-1结束),降序输出。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Experiment_2
{
internal class Program3
{
static void Main(string[] args)
{
/*
* 编写控制台程序,利用ArrayList进行数组操作,
* 随机输入若干个数据(以-1结束),降序输出。
*/
ArrayList MyArrayList = new ArrayList();
Console.WriteLine("请输入数据,以 -1 结束");
int c = int.Parse(Console.ReadLine());
while ( c != -1)//用循环控制是否结束
{
MyArrayList.Add(c);//加入list
c = int.Parse(Console.ReadLine());
}
Console.Write("输入的数据为:");
foreach(int a in MyArrayList)//循环遍历list并且输出
{
Console.Write(a+"\t");
}
Console.WriteLine();
MyArrayList.Sort();//排序
MyArrayList.Reverse(); //将ArrayList中元素的顺序反转。
Console.Write("排序后的数据为:");
foreach (int a in MyArrayList)//循环遍历list并且输出
{
Console.Write(a + "\t");
}
Console.WriteLine();
}
}
}
}