jQuery火箭图标返回顶部代码 - 站长素材

期末练习

知识储备

//字符串转化为Char数组
1. str.ToCharArray();

//字符串转化为double数组
2. Console.ReadLine().Split(' ').Select(double.Parse).ToArray();

//正则表达式替换(一至多个空格,替换为一个空格)
3. Regex.Replace(str, @"\s+", " ");

//判空抛异常
4. throw new ArgumentNullException(nameof(name))

//字符串类型转化为日期类型,并判断格式是否正确
//将old_str转化为日期格式,转化失败返回Flase,成功则返回new_Time。
5. DateTime.TryParse(old_str, out new_Time);

//提取字符串中的元素(从第四个元素开始,后面两个)
6.str.Substring(4,2);

1.逆序输出

string str = textBox1.Text;
char[] arr = str.ToCharArray();
string result = "";
for(int i = arr.Length-1; i >= 0; i--)
{
    result += arr[i];
}
textBox2.Text = result;

-------------------------------------------

string str = textBox1.Text;
char[] arr = str.ToCharArray();
Array.Reverse(arr);
string newStr = new string(arr);
textBox2.Text = newStr;

2.编程判断a、b、c的值是否构成三角形,如构成三角形,则计算并输出三角形的面积,否则输出“不能构成三角形”

 //s=0.5*(a+b+c)   area= s*(s-a)*(s-b)*(s-c)再开根
 double a, b, c;
 a = double.Parse(textBox1.Text);
 b = double.Parse(textBox2.Text);
 c = double.Parse(textBox3.Text);        
 if (a + b > c && a + c > b && b + c > a)
 {
     double s = (a + b + c) / 2;
     double area = Math.Sqrt(s * (s - a) * (s - b) * (s - c));
     MessageBox.Show("三角形的面积为:" + area.ToString());
 }
 else
 {   //消息窗口(对话框)
     MessageBox.Show("不能构成三角形");
 }   

3.输入 10个数,计算平均值,统计低于平均值数据个数并把低于平均值的数据输出。

public void Print1()
{
    Console.WriteLine("请输入10个数字,以空格分隔:");
    //控制台输入10个数字,select().ToArray()方法将字符串转换为double数组
    double[] input = Console.ReadLine().Split(' ').Select(double.Parse).ToArray();
    double avg,num=0;
    int sum = 0;
    for (int i=0;i<input.Length;i++)
    {
        num += input[i];
    }
    avg = num / input.Length;
    Console.WriteLine("平均值为:" + avg);
    foreach(double i in input)
    {
        if (i < avg)
        {
            sum++;
            Console.Write(i + " ");
        }
    }
    Console.WriteLine("\n小于平均值的个数为:" + sum);
}

4.输入一些整数,分别统计正数、负数、0的个数,并输出他们的和。

public void Print2()
{
    Console.WriteLine("请输入任意整数,以空格分隔:");
    string str = Console.ReadLine();
    int[] input = str.Split(' ').Select(int.Parse).ToArray();
    int zhengshu=0,fushu=0,zero=0;
    int sum = 0;

    foreach(int i in input){
        if (i>0)
        {
            zhengshu++;
        }else if (i < 0)
        {
            fushu++;
        }
        else
        {
            zero++;
        }
        sum += i;
    }
    Console.WriteLine("正数个数:{0}\n负数的个数:{1}\n零的个数:{2},和为:{3}",zhengshu,fushu,zero,sum);

}

5.输入一班 20名同学的数学成绩,求出该班数学成绩的最高分、最低分、平均分以及低于平均分的人数。

public void Print3()
{
    Console.WriteLine("请输入20名同学的成绩,以空格分隔:");
    double[] input = Console.ReadLine().Split(' ').Select(double.Parse).ToArray();
    double avg, num = 0;
    int sum = 0;
    for (int i = 0; i < input.Length; i++)
    {
        num += input[i];
    }
    double max =input[0],min = input[0];
    avg = num / input.Length;
    Console.WriteLine("平均值为:" + avg);
    foreach (double i in input)
    {
        if (i < avg)
        {
            sum++;
        }
        if (i<min)
        {
            min = i; 
        }
        if (i>max)
        {
            max = i;
        }
    }
    Console.WriteLine($"该班级的最低分:{min},最高分:{max},平均分:{avg}和低于平均分的人数为:{sum}");

}

6.输入一些正整数,统计奇数和偶数的个数并输出,并输出所有奇数和偶数

public void Print4()
{
    Console.WriteLine("请输入任意正整数,以空格分隔:");
    string str = Console.ReadLine();
    int[] input = str.Split(' ').Select(int.Parse).ToArray();
    int oushu = 0, jishu = 0;
    for (int i = 0; i < input.Length; i++)
    {
        if (input[i] % 2 == 0)
        {
            oushu++;
            Console.Write(input[i] + " ");
        }
        else
        {
            jishu++;
            Console.Write(input[i] + " ");
        }
    }
    Console.WriteLine("偶数个数:{0}\n奇数个数:{1}", oushu, jishu);
}

7.写方法,统计一段英文短文中单词的平均长度和最长的单词。要求:单词之间由若干个空格隔开。

public void Print1()
{
    int max=0, avg,sum=0;
    string Max_World = "";
    string str = " This   is   a  sample  string  with   multiple   spaces. ";
    /*这里利用了正则表达式的替换功能:
    Regex.Replace方法用于在给定的输入字符串中进行替换操作。
    正则表达式\s + 表示匹配一个或多个空白字符(包括空格、制表符、换行符等),
    将其替换为单个空格,从而实现去掉多余空格只保留一个的目的。*/
    string newstr = Regex.Replace(str, @"\s+", " ");

    string[] strList = newstr.Split(' ').ToArray();
    for (int i = 0; i < strList.Length; i++)
    {
        if (strList[i].Length > max)
        {
            max = strList[i].Length;
            Max_World = strList[i];
        }
        sum += strList[i].Length;
    }
    avg = sum/strList.Length;
    Console.WriteLine($"长度最长的单词是: {Max_World}, 其长度为:{max},单词平均长度为{avg}" );

}

8.写方法,分别统计字符串中英文字母和数字字符的个数。

public void Print2()
{
    string str = "as35dfa1s54w3545w345455e/,.[//";
    int num_shuzi = 0, num_yingwen = 0;
    for (int i=0;i<str.Length;i++)
    {
        if (str[i]>='0'&&str[i]<='9')
        {
            num_shuzi++;
        }
        if (str[i]>='a'&&str[i]<='z' || str[i]>='A'&&str[i]<='Z')
        {
            num_yingwen++;
        }
    }
    Console.WriteLine($"字符串中的英文字母的个数为{num_yingwen},数字字符的个数为:{num_shuzi}");

}

学生类(简易版)

创建一个学生类(Student),要求:
(1)有及其子类研究生类(Graduate),学生类包含私有成员字段 name,credit
并包含其属性 Name,Credit;
(2)有自己的无参、有参构造方法;
(3)有输出信息的方法。
创建其子类研究生类,要求:
(1)有私有变量 postCredit;并包含其属性 PostCredit;
(2)有自己的无参、有参构造方法;
(3)有输出信息的方法。
创建一个研究生对象并设置其 postcredit,另建立学生数组(研究生作为其一
个元素),要求打印输出该学生数组的姓名和学分信息。

    //主类
    internal class Program
    {
        static void Main(string[] args)
        {
            // 创建一个研究生对象并设置其postCredit
            Graduate gra = new Graduate("张三",120,130);
            // 创建学生数组,包含研究生对象作为其中一个元素
            Student[] stu = new Student[1];
            stu[0] = gra;
            // 遍历学生数组,并调用其方法
            foreach (Student i in stu)
            {
                Console.WriteLine(i.ShowInfo());
            }
            Console.ReadKey();
        }

    //学生类(基类)
    internal class Student
    {
        //姓名和学分
        private string name;
        private double credit;

        public string Name
        {
            get { return name; }
            set{if (value != null){this.name = value;}}
        }
        public double Credit{
            get { return credit; }
            set { this.credit = value; }
        }
        public Student()
        {
            this.name = "NULL";
            this.credit = 0;
        }
        public Student(string name, double credit)
        {
            this.name = name ?? throw new ArgumentNullException(nameof(name));
            this.credit = credit;
        }

        public string ShowInfo()
        {
            return $"学生的姓名为:{name}学号为:{credit}";
        }
    }

    //研究生类(子类)
    internal class Graduate : Student
    {
        //研究生学分
        private int postCredit;
        public Graduate(){  }
        //base()方法:重构父类构造函数
        public Graduate(string name, double credit, int postCredit) : base(name,credit)
        {
            this.postCredit = postCredit;
        }
        public int PostCredit
        {
            get => postCredit; 
            set =>postCredit = value; 
        }
        public void PrintInfo()
        {
            return "该学生信息:"+ShowInfo()+",研究生学分:"+postCredit;
        }
    }

图书类(优化版)

创建一个图书类,要求:
(1)封装图书的名称、ISBN、出版社、出版日期等信息;
(2)包含有参和无参的构造函数;
(3)名称不能为空而且长度小于 50;出版日期不能为空,而且必须是
日期;
(4)具有一个能输出书籍信息的方法;
创建一个图书数组,并为数组每个元素赋值,要求输出数组中书籍的信息。

//主类
internal class Program
{
    static void Main(string[] args)
    {
        //录入书籍的数量
        Books[] books = new Books[3];
        for (int i = 0; i < 3; i++)
        {
            //显示录入的提示信息(实时变化)
            Console.WriteLine("请输入" + (i + 1) + "本书籍的信息:");
            Console.WriteLine("请输入书名:");
            string name = Console.ReadLine();
            Console.WriteLine("请输入书籍的ISBN码:");
            string isbn = Console.ReadLine();
            Console.WriteLine("请输入出版社:");
            string chubanshe = Console.ReadLine();
            Console.WriteLine("请输入出版日期(YYYY-MM-DD):");
            string dateStr = Console.ReadLine();
            DateTime date;
            //验证日期格式是否正确;若格式不正确,则执行i--,回到上一轮循环,重新输入
            if(DateTime.TryParse(dateStr,out DateTime date1))
            {
                date = date1;
            }
            else
            {
                Console.WriteLine("日期格式不正确,请重新输入!");
                i--;
                continue;
            }
            books[i] = new Books(name, isbn, chubanshe,date);
        }
        Console.WriteLine("以下是书籍信息:");
        foreach (Books book in books)
        {
            book.ShowInfo();
        }
        Console.ReadKey();
    }
}

     //图书馆类
    internal class Books
    {
        private string name;
        private string isbn;
        private string chubanshe;
        private DateTime publishDate;

        //| 构造函数 |初始化默认值
        public Books()
        {
            this.name = "没有信息";
            this.isbn = "没有信息";
            this.chubanshe = "没有信息";
            this.publishDate = DateTime.Now;
        }
        public Books(string name, string isbn, string chubanshe, DateTime publishDate)
        {
            if(name == null)
            {
                Console.WriteLine("书名不能为空!");
            }
            else
            {
                this.name = name;
            }
            this.isbn = isbn;
            this.chubanshe = chubanshe;
            //判断出版日期是否为空&& 格式是否正确
            //使用DateTime.TryParse(old_str, out new_str)方法进行格式转换并判断格式
            if (publishDate == null && DateTime.TryParse(publishDate.ToString("yyyy-MM-dd"), out publishDate))
            {
                Console.WriteLine("出版日期不能为空!");
            }
            else
            {
                this.publishDate = publishDate;
            }
        }

        public void ShowInfo()
        {
            Console.WriteLine($"书名:{name}、ISBN:{isbn}、出版社:{chubanshe}、出版日期:"+publishDate.ToString("yyyy-MM-dd") );
        }
    }

在 C#中,Math类提供了很多常用的数学方法,以下是一些主要的:

  1. Math.Abs(double value):返回指定数字的绝对值。例如,Math.Abs(-5)返回 5
  2. Math.Ceiling(double value):返回大于或等于指定数字的最小整数。例如,Math.Ceiling(4.2)返回 5。
  3. Math.Floor(double value):返回小于或等于指定数字的最大整数。例如,Math.Floor(4.8)返回 4。
  4. Math.Round(double value):将值四舍五入到最接近的整数或指定的小数位数。例如,Math.Round(4.5)返回 4 和 5 之间的偶数 4;Math.Round(4.6)返回 5。
  5. Math.Pow(double x, double y):返回指定数字的指定次幂。例如,Math.Pow(2, 3)返回 8。
  6. Math.Sqrt(double value):返回指定数字的平方根。例如,Math.Sqrt(9)返回 3。`
  7. Math.Max(int value1, int value2)等一系列重载方法:返回两个指定数字中的较大值。例如,Math.Max(5, 8)返回 8。 `
  8. Math.Min(int value1, int value2)等一系列重载方法:返回两个指定数字中的较小值。例如,Math.Min(5, 8)返回 5。
posted @ 2025-01-05 01:10  时移之人  阅读(69)  评论(0)    收藏  举报
Live2D