C# 实验一

实验一 Visual Studio 2022集成开发环境使用及C#语言基本语法应用练习

一、实验目的

  1. 熟悉集成开发环境Microsoft Visual Studio 2019的基本命令及功能键,熟悉常用功能菜单命令。
  2. 学习C#程序开发过程(编辑、编译、生成、运行和调试等),掌握使用Visual Studio 环境开发与调试程序的方法与技巧。
  3. 掌握C#语言基本程序结构的特点,掌握程序控制语句的使用方法。
  4. 掌握C#常用类的使用,重点掌握字符串类、日期时间类和随机数类的使用。
    二、实验内容

1.编写一个控制台程序,输入一个学生的成绩,输出其等级(优:>=90;良:>=80;中:>=70;及格:>=60;不及格:<60)。
说明:若输入的成绩超出0-100时,输出:“输入成绩错误”。

源程序一:
namespace Experiment_1
{
    internal class Program
    {
        //这是整个作业的调用入口
        static void Main(string[] args)
        {
            //作业一
            results r=new results();
            r.MyResults();//输出相应的信息
        }
    }
}
源程序二:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Experiment_1
{
    internal class results
    {
        public void MyResults()
        {

            Console.WriteLine("请输入一个学生的成绩");
            int score = int.Parse(Console.ReadLine());
            if (score > 100 || score < 0)
            {
                Console.WriteLine("输入成绩错误");
            }
            else if (score >= 90)
            {
                Console.WriteLine("优");
            }
            else if (score >= 80)
            {
                Console.WriteLine("良");
            }
            else if (score >= 70) { Console.WriteLine("中"); }
            else if (score >= 60) { Console.WriteLine("及格"); }
            else
            {
                Console.WriteLine("不及格");
            }


        }
    }
}

实验结果:
键盘输入98 程序输出 优
在这里插入图片描述

2.Sting类练习。
编写一个控制台程序,接收一个长度大于3的字符串(且字符串中包含逗号“,”),完成下列功能,运行效果如图1-1所示。
(1)输出字符串的长度。
(2)输出字符串中第一个出现字母a的位置(若输入的字符串没有字母a,则输出“未找到字母a”)。
(3)在字符串的第3个字符后面插入子串“hello”,输出新字符串。
(4)将字符串hello替换为“me”,输出新字符串。
(5)以字符逗号“,”为分隔符,将字符串分离,并输出分离后的字符串。

图1-1  String类练习

源程序一:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Experiment_1
{
    internal class Program
    {
        //这是整个作业的调用入口
        static void Main(string[] args)
        {
//作业一
            //results r=new results();
            //r.MyResults();



            //作业二
            StringEx stringEx=new StringEx();
            stringEx.MyStringEx();
        }
    }
}



源程序二:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Experiment_1
{
    internal class StringEx
    {
        public void MyStringEx()
        {
            // 编写一个控制台程序,接收一个长度大于3的字符串(且字符串中包含逗号“,”),
            // 完成下列功能,运行效果如图1 - 1所示。
            Console.WriteLine("请输入长度大于3的字符串");
           string str= Console.ReadLine();
            Console.WriteLine("str字符串长度为: "+str.Length); //(1)输出字符串的长度。
            //(2)输出字符串中第一个出现字母a的位置(若输入的字符串没有字母a,则输出“未找到字母a”)。
           if(str.IndexOf('a') > 0)
            {
                Console.WriteLine("字符a出现的位置是"+(str.IndexOf('a')+1));
            }
            else
            {
                Console.WriteLine("未找到字母a");
            }
            //(3)在字符串的第3个字符后面插入子串“hello”,输出新字符串。
            Console.WriteLine("第3个字符后面插入后的结果:" + str.Insert(2, "hello"));

            //(4)将字符串hello替换为“me”,输出新字符串。
            string newstr = str.Insert(2, "hello").Replace("hello", "me");
            Console.WriteLine("将字符串hello替换为“me”的结果:" + newstr);

            //(5)以字符逗号“,”为分隔符,将字符串分离,并输出分离后的字符串。
            string[] newstr2 = str.Split(',');
            Console.Write("分离后的字符串: ");
            foreach (string a in newstr2)
            {
                Console.Write( a+" ");
            }
            Console.WriteLine("");

        }
    }
}

实验结果:
在这里插入图片描述

3.DateTime类练习。
编写一个控制台程序, 将当前日期和时间按要求输出,如图1-2所示。

提示:此题实现时采用日期时间类的Now属性的常用方法,各方法格式为:
DateTime.Now.方法名称(参数列表)
日期时间类的Now属性的常用属性,格式为:DateTime.Now.属性名称

源程序一:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Experiment_1
{
    internal class Program
    {
        //这是整个作业的调用入口
        static void Main(string[] args)
        {
            //作业一
            //results r=new results();
            //r.MyResults();

            //作业二
            //StringEx stringEx=new StringEx();
            //stringEx.MyStringEx();

            //作业三
            MyDateTime mt=new MyDateTime();
            mt.ShowMyDateTime();
        }
    }
}


源程序二:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Experiment_1
{
    internal class MyDateTime
    {
        public void ShowMyDateTime()
        {
            DateTime date1 = DateTime.Now;//2022/8/23 20:29:16

            int year = date1.Year;//年
            int month = date1.Month;//月
            int day = date1.Day;//日
            Console.WriteLine("当前日期:{0}年{1}月{2}日", year, month, day);
            Console.WriteLine("当前日期:{0}年{1}月{2}日", year, month, day);
            Console.WriteLine("当前日期:{0}/{1}/{2}", year, month, day);
            Console.WriteLine("当前日期时间" + date1);
            Console.WriteLine("当前年份:{0}年", year);
            Console.WriteLine("当前月份:{0}月", month);
            DateTime date2 = date1.AddDays(10);
             Console.WriteLine("10天后的日期:"+ date2);
        }
    }
}

实验结果:

在这里插入图片描述

4.设计控制台程序,声明一个学生结构类型Stud,包含学号,姓名和出生日期成员;定义Stud结构的两个学生变量S1和S2并赋值,求他们出生在星期几及他们出生相差的天数,如图1-3所示。

1-3 运行效果图
源程序一:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Experiment_1
{
    internal class Program
    {
        //这是整个作业的调用入口
        static void Main(string[] args)
        {
            //作业一
            //results r=new results();
            //r.MyResults();

            //作业二
            //StringEx stringEx=new StringEx();
            //stringEx.MyStringEx();

            //作业三
            //MyDateTime mt=new MyDateTime();
            //mt.ShowMyDateTime();

            //作业四
            Stud stud1 = new Stud(001, "张三", new DateTime(2022, 8, 23));
            Stud stud2 = new Stud(002, "李四", new DateTime(2022, 9, 1));
            stud1.GetWeek();//得到星期几
            stud2.birthday();//出生日期
            stud2.GetWeek();
            stud1.ChaDay(stud2.GetDate());
        }
    }
}


源程序二:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Experiment_1
{
    internal class Stud
    {
        //设计控制台程序,声明一个学生结构类型Stud,包含学号,姓名和出生日期成员;
        //    定义Stud结构的两个学生变量S1和S2并赋值,
        //    求他们出生在星期几及他们出生相差的天数,
        int id;//学号
        string name;//姓名
        DateTime date;//出生日期

        public Stud(){}
        public Stud(int id,string name, DateTime date) { 
            this.id = id;
            this.name = name;
            this.date = date;

        }
        public void GetWeek()
        {
            //求他们出生在星期几
            Console.WriteLine(this.name+" 出生在 "+this.date.DayOfWeek);
            

        }
        public void ChaDay(DateTime date)
        {
            //输出相差的天数


            Console.WriteLine("他们出生相差的天数"+ Math.Abs(this.date.Subtract(date).Days)+"天");
          

        }
        public DateTime GetDate()
        {
            return this.date;    
        }
        public void birthday()
        {
            int year = this.date.Year;
            int month = this.date.Month;
            int day = this.date.Day;    
            Console.WriteLine(this.name+"的生日:{0}年{1}月{2}日",year,month,day);
        }
    }
}

实验结果:

在这里插入图片描述

  • 6
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值