欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

第五周作业

程序员文章站 2024-03-21 23:53:34
...

1、由用户输入银行的英文简称,使用if ... else if 语句判断并输出银行的中文全称。

要点:分别使用if ... else if 语句和switch语句

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

namespace Demo_ifelse
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入银行的英文简称:");
            string bank = Console.ReadLine();
            if (bank == "ICBC")
                Console.WriteLine("中国工商银行");
            else if (bank == "CBC")
                Console.WriteLine("中国建设银行");
            else if (bank == "ABC")
                Console.WriteLine("中国农业银行");
            else if (bank == "BOC")
                Console.WriteLine("中国银行");
            else
                Console.WriteLine("银行简称输入有误!");

            Console.ReadKey();
        }
    }
}

2、由用户输入银行的英文简称,使用switch(...)语句判断并输出银行的中文全称。

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

namespace Demo_ifelse
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入银行的英文简称:");
            string bank = Console.ReadLine();
            if (bank == "ICBC")
                Console.WriteLine("中国工商银行");
            else if (bank == "CBC")
                Console.WriteLine("中国建设银行");
            else if (bank == "ABC")
                Console.WriteLine("中国农业银行");
            else if (bank == "BOC")
                Console.WriteLine("中国银行");
            else
                Console.WriteLine("银行简称输入有误!");

            Console.ReadKey();
        }
    }
}

3、设计一个程序计算1~100之间的奇数和、偶数和。

4、设计一个程序打印一张九九选乘法表。

第五周作业

5、设计一程序,演示一维数组、二维数的定义、初始化、数组元素的赋值与取数。

    要点:定长多维数组、非定长多维数组的定义、初始化、数组行数和列数的获取。Array.Length与Array.GetLength(0)、Array.GetLength(1)的含义及用法。

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

namespace Demo_Array2
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] b = { 13,2,5,8,4,26,10};

            int[,] arr1 = new int[2,3];//定长二维数组
            int[][] arr2 = new int[2][];//非定长二维数组
            arr2[0] = new int[3];
            arr2[1] = new int[8];

            int[,] a = new int[2,3] { {78,2,3},{4,26,6}};

            for(int i=0;i<arr1.GetLength(0);i++)
            {
                for (int j = 0; j < arr1.GetLength(1); j++)//取得定义数组元素的列数
                {
                    arr1[i, j] = i + j;
                    Console.Write("arr1[{0},{1}]={2}  ", i, j, arr1[i, j]);
                }
                Console.WriteLine();
            }

            foreach (int x in a)
            {
                {
                    Console.Write(x+"  ");
                }
                Console.WriteLine();
            }


            Console.WriteLine("arr2.Length={0},arr2.GetLength(0)={1},arr2[0].GetLength(1)={2},arr2[0].GetLength(1)={3}",
                arr2.Length, arr2.GetLength(0), arr2[0].GetLength(0), arr2[1].GetLength(0));
            //Console.ReadKey();

            
            for (int i = 0; i < arr2.GetLength(0); i++)
            {
                for (int j = 0; j < arr2[i].GetLength(0); j++)//取得非定长数组某一行的元素个数
                {
                    arr2[i][j] = i + j;
                    Console.Write("arr2[{0}][{1}]={2}  ", i, j, arr2[i][j]);
                }
                Console.WriteLine();
            }


            Console.ReadKey();
        }
    }
}

6、设计一个程序,演示ArrayList的用法(定义、排序、增加、插入、删除、查找等)。

   要点:需要引用下面的命名空间:

using System.Collections;

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

namespace Demo_ArrayList
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] a = {2,5,1,19,81,23,4,6};

            ArrayList List = new ArrayList(a); 

            Console.WriteLine("数组的原始值:");
            for(int i=0;i<a.Length;i++)
            {
                Console.Write(a[i]+" ");
            }
            Console.WriteLine();

            Console.WriteLine("List的原始值:");
            foreach (int x in List)
            {
                Console.Write(x+" ");
            }
            Console.WriteLine();
			
            List.Add(66);
            List.Insert(3, 44);
            Console.WriteLine("List的新增和插入数据后:");
            foreach (int x in List)
            {
                Console.Write(x+" ");
            }
            Console.WriteLine();
			
            List.Remove(5);
            List.RemoveAt(3);

            Console.WriteLine("List的删除2个数据后:");
            foreach (int x in List)
            {
                Console.Write(x+" ");
            }

            Console.WriteLine();

            List.RemoveRange(3, 2);
            Console.WriteLine("List的删除一段数据:");
            foreach (int x in List)
            {
                Console.Write(x+" ");
            }

			
            Console.WriteLine();
            List.Sort();
            Console.WriteLine("List排序之后的值:");
            foreach (int x in List)
            {
                Console.Write(x + " ");
            }

            Console.ReadKey();

        }
    }
}

7、设计一个程序,演示HashTable的用法(Contains,ContainsKey,ContainsValue的用法

    要点:如何进行遍历?

Hashtable hashtable = new Hashtable(); //创建Hashtable对象

hashtable.Add("id", "BH0001"); //Hashtable中添加元素

hashtable.Add("name", "TM");

hashtable.Add("sex", "");

Console.WriteLine("\t \t ");

foreach (DictionaryEntry dicEntry in hashtable)

{

     Console.WriteLine("\t " + dicEntry.Key + "\t " + dicEntry.Value);

}

    举个例子:

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

namespace Demo_HashTable
{
    class Program
    {
        //本程序用于演示哈希表的用法,小宇飞刀 2019-03-27
        static void Main(string[] args)
        {
            Hashtable srvInfo = new Hashtable(); //哈希表经常用于保存或读取软件的配置信息,比如要存储数据库的连接信息等

            srvInfo.Add("SrvIp", "172.18.4.100");
            srvInfo.Add("DbName", "jfglxt");
            srvInfo.Add("UserName", "sa"); 
            srvInfo.Add("UserPwd", "aaa@qq.com");

            foreach (DictionaryEntry dicEntry in srvInfo)
               Console.WriteLine(dicEntry.Key + "\t " + dicEntry.Value);

            Console.WriteLine();//输出一个换行

            Console.WriteLine("SrvIp={0}",srvInfo["SrvIp"]);
            Console.WriteLine("DbName={0}",srvInfo["DbName"]);
            Console.WriteLine("UserName={0}", srvInfo["UserName"]);
            Console.WriteLine("UserPwd={0}", srvInfo["UserPwd"]);

            Console.WriteLine();//输出一个换行
            if (srvInfo.Contains("DbName")) //等价于srvInfo.ContainsKey("DbName")
            {
                Console.WriteLine("Key为DbName的键值已找到!");
            }
            if (srvInfo.ContainsValue("jfglxt"))
            {
                Console.WriteLine("Vlaue为jfglxt的内容已找到!");
            }
            System.Console.ReadKey();
        }
        
    }
}

8、演示DEMO源代码在github上的仓库地址:

https://github.com/xieyunc/csharp_teach.git