C#--第三周实验--任务3--输入学号和姓名,输出学号为奇数的所有学生。(控制台应用)

来源:互联网 发布:如何查看mac硬盘容量 编辑:程序博客网 时间:2024/05/18 09:19

/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:输入学号和姓名,对不存在的学号加到hashtable类的实例中,对存在学号给出提示。结束输入后,输出学号为奇数的所有学生。

* 作 者: 雷恒鑫
* 完成日期: 2012 年 09 月 15 日
* 版 本 号: V1.0
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:

* 程序头部的注释结束

*/

 

下面的程序是输出所有学生的信息:

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace The_experiment__of__three__week{    class Program    {        static void Main(string[] args)        {            int count;            bool b = true;            Console.Write("请您输入您想保存的学生数:");            count = int.Parse(Console.ReadLine());            hashtable[] h = new hashtable[count];            int i = 1, j = i;            while (j > 0)            {                j = i;                if (i <= count)                {                    Console.Write("请您输入第{0}个学生的学号:", i);                    int number = int.Parse(Console.ReadLine());                    Console.Write("请您输入第{0}个学生的姓名:", i);                    String name = Console.ReadLine();                    for (int k = 0; k < i - 2; ++k)                    {                        if (h[k].number1().Equals(number))                        {                            Console.WriteLine("您已经输过该同学的信息了,请不要重复输入!");                            b = false;                            break;                        }                    }                    if (b)                    {                        h[i - 1] = new hashtable();                        h[i - 1].input(number, name);                    }                    ++i;                }                else                {                    Console.WriteLine("您输入的学生数已达到您刚才预先设定值,请停止输入!");                    break;                }                Console.WriteLine();                Console.WriteLine("想结束输入请按 ‘0’   继续输入请按 ‘1’ ");                j = int.Parse(Console.ReadLine());            }            for (int n = 0; n < count; ++n)            {                h[n].output();            }            Console.ReadKey();        }    }    class hashtable    {        public int number;        public string name;        public hashtable(int number, string name)        {            number = 0;            name = null;        }        public hashtable()        {            number = 0;            name = null;        }        public void input(int number1, string name1)        {            number = number1;            name = name1;        }        public int number1()        {            return number;        }        public void output()        {            Console.WriteLine("  学号         姓名");            Console.WriteLine("  {0}        {1}", number, name);        }    }}

 

运行结果:

 

 

 输出所有学号是奇数的学生       完整的程序为:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace The_experiment__of__three__week{    class Program    {        static void Main(string[] args)        {            int count;            bool b = true;            Console.Write("请您输入您想保存的学生数:");            count = int.Parse(Console.ReadLine());            hashtable[] h = new hashtable[count];            int i = 1, j = i;            while (j > 0)            {                j = i;                if (i <= count)                {                    Console.Write("请您输入第{0}个学生的学号:", i);                    int number = int.Parse(Console.ReadLine());                    Console.Write("请您输入第{0}个学生的姓名:", i);                    String name = Console.ReadLine();                    for (int k = 0; k < i - 2; ++k)                    {                        if (h[k].number1().Equals(number))                        {                            Console.WriteLine("您已经输过该同学的信息了,请不要重复输入!");                            b = false;                            break;                        }                    }                    if (b)                    {                        h[i - 1] = new hashtable();                        h[i - 1].input(number, name);                    }                    ++i;                }                else                {                    Console.WriteLine("您输入的学生数已达到您刚才预先设定值,请停止输入!");                    break;                }                Console.WriteLine();                Console.WriteLine("想结束输入请按 ‘0’   继续输入请按 ‘1’ ");                j = int.Parse(Console.ReadLine());            }            Console.WriteLine();            Console.WriteLine("输出学号为奇数的所有学生信息为:");            for (int n = 0; n < count; ++n)            {                if(h[n].number1()%2!=0)                h[n].output();            }            Console.ReadKey();        }    }    class hashtable    {        public int number;        public string name;        public hashtable(int number, string name)        {            number = 0;            name = null;        }        public hashtable()        {            number = 0;            name = null;        }        public void input(int number1, string name1)        {            number = number1;            name = name1;        }        public int number1()        {            return number;        }        public void output()        {            Console.WriteLine("  学号         姓名");            Console.WriteLine("  {0}        {1}", number, name);        }    }}


 

运行结果:

 

 

经验积累:

 

1.不仅要将数组名实例化,还要将数组中的每一个元素实例化。否则在调用类中数组的函数的时候会发生错误。

 

hashtable[] h = new hashtable[count]; if (b) {      h[i - 1] = new hashtable();      h[i - 1].input(number, name);}