c# 建立一个学生类,包括私有字段:学号、姓名、班级、年龄

  1. 建立一个学生类,包括私有字段:学号、姓名、班级、年龄,包括公共属性:学号、姓名、班级、年龄
  2. 实例化4个学生对象,并存储到学生类型数组中
  3. 用户输入要查询的学号,搜索数组中是否有相应学生,如果有,在屏幕输出此学生所有信息;如果没有,屏幕输出查无此生的提示。
  4. 在屏幕输出所有学生信息
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test
{
    class Program
    {
        class Student
        { //创建一个学生类,有学号,姓名,班级,年龄
            private int id;
            private string name;
            private string clas;
            private int age;

            public Student(int id, string name, string clas, int age)
            //构造函数,用来初始化
            {
                this.id = id;
                this.name = name;
                this.clas = clas;
                this.age = age;
            }
            //定义属性
            /*属性是c#特有的,通常是变量的首字母大写
             get用来返回类成员的值
             set用来给类成员赋值,当然也可以对值进行判断,用value进行赋值*/
            public int Id
            {
                get
                {
                    return id;
                }
                set
                {
                    id = value;
                }
            }
            public string Name
            {
                get
                {
                    return name;
                }
                set
                {
                    name = value;
                }
            }
            public string Clas
            {
                get
                {
                    return clas;
                }
                set
                {
                    clas = value;
                }
            }
            public int Age
            {
                get
                {
                    return age;
                }
                set
                {
                        age = value;
                }
            }
        }
        static void Main(string[] args)
        {
            /*
             定义四个对象,进行初始化,之后将其复制到数组中
             然后输入要查新的id,利用循环和数组中对每一个成员比较
            */
            Student s1 = new Student(03061, "詹姆斯", "计科1", 36);
            Student s2 = new Student(03062, "库里", "计科2", 31);
            Student s3 = new Student(03063, "利拉德", "计科3", 32);
            Student s4 = new Student(03064, "欧文", "计科4", 28);
            s4.Age = 2;
            Student[] s = new Student[4];
            s[0] = s1;
            s[1] = s2;
            s[2] = s3;
            s[3] = s4;
            int id_find;
            int flag = 0;
            id_find = int.Parse(Console.ReadLine());
            for (int i = 0; i < 4; i++)
            {
                if (id_find == s[i].Id)
                {
                    Console.WriteLine("学生id:" + s[i].Id);
                    Console.WriteLine("学生姓名:" + s[i].Name);
                    Console.WriteLine("学生班级:" + s[i].Clas);
                    Console.WriteLine("学生年龄:" + s[i].Age);
                    flag = 1;

                }
            }

            if (flag == 0)
            {
                Console.WriteLine("查无此生!");
            }
            Console.WriteLine("--------------------------------");
            Console.WriteLine("输出全部学生:");
            for (int i = 0; i < 4; i++)
            {
                Console.WriteLine("学生id:" + s[i].Id);
                Console.WriteLine("学生姓名:" + s[i].Name);
                Console.WriteLine("学生班级:" + s[i].Clas);
                Console.WriteLine("学生年龄:" + s[i].Age);
                Console.WriteLine();
            }
            Console.ReadLine();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值