深入.NET平台和C#编程第二张上机练习2

本文通过一个具体的C#程序实例介绍了如何使用Random类生成随机数,并展示了如何创建对象数组来存储不同类型的数据。此外,还演示了如何为数组中的每个元素设置属性并进行读取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

namespace instance
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] num = new int[3];


            //计算出四位的随机数
            Random ran = new Random();
            for (int i = 0; i < num.Length; i++)
            {
                num[i] = ran.Next(1000, 10000);
            }
            //利用数组储存数据
            computer[] com = new computer[3];
            computer com1 = new computer("hpCQ-217TX", "2013-10-05", "");
            computer com2 = new computer("Mc240CH", "2013-10-05", "");
            computer com3 = new computer("SYNW18H", "2013-10-05", "");

            com[0] = com1;
            com[1] = com2;
            com[2] = com3;
            Console.WriteLine("************************设置计算机ID前*************************" + "\n");
            Console.WriteLine("计算机型号\t计算机ID\t购买时间");

            foreach (computer item in com)
            {
                Console.WriteLine(item.Type + "\t\t" + item.ID + "\t\t" + item.Time);
            }
            //拼接 计算机id
            for (int i = 0; i < com.Length; i++)
            {
                com[i].ID = com[i].Type + "-" + num[i];
            }

            Console.WriteLine("************************设置计算机ID后*************************" + "\n");

            foreach (computer item in com)
            {
                Console.WriteLine(item.Type + "\t\t" + item.ID + "\t" + item.Time);
            }
            Console.ReadLine();




        }
    }
}

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

namespace instance
{
    class computer
    {


        public string Type { get; set; }
        public string Time { get; set; }
        public string ID { get; set; }


        public computer() { }

        public computer(string TypeId, string BuyTime, string ID)
        {
            this.Type = TypeId;
            this.Time = BuyTime;
            this.ID = ID;


        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值