C# 课堂练习实验题(持续更新)

2 篇文章 0 订阅

C# 课堂练习

这里放上选修课上学习C#老师给的作业答案,供以后参考,因为是应付作业所以有更好的算法也没有放上去,以后再说吧

打印100-200之间的所有素数

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

namespace 输出素数
{
    class Program
    {
        static void Main(string[] args)
        {

            for(int i=101; i <= 200; i++)
            {
                int p = 0;
               for (p = 2; p < i; p++)   //这里应该是利用二分之一的计算量应该更小
                {
                    if (i%p == 0)
                    {
                        break;
                    }
                    
                }
                if (i==p)   
                {
                    Console.WriteLine(i.ToString());
                }
            }
            Console.ReadKey();
        }
    }
}

随机生成100个两位正整数,并统计出其中小于等于40、大于40小于等于70以及大于70的数据个数。

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

namespace 统计数据
{
    class Program
    {
        /// <summary>
        /// 随机生成100个两位正整数,并统计出其中小于等于40、大于40小于等于70以及大于70的数据个数。
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            Random rd = new Random();
            int temp;
            int xy_40=0; //统计小于40的数
            int fw40_70=0; //统计40~70的数
            int dy_70=0; //统计大于70的数
            for (int i =0; i < 100; i++)
            {
                temp = rd.Next(10, 99);
                if (temp < 40)
                {
                    xy_40++;
                }
                else if (temp>=40 && temp <= 70)
                {
                    fw40_70++;
                }
                else
                {
                    dy_70++;
                }
                Console.WriteLine(temp);
            }
            Console.WriteLine("小于40的个数为"+xy_40.ToString());
            Console.WriteLine("40~70的个数为"+fw40_70.ToString());
            Console.WriteLine("大于70的个数为"+dy_70.ToString());
            Console.ReadKey();
        }
    }
}

冒泡排序

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

namespace program18
{
    class Program18
    {
        static void Main(string[] args)
        {
            int[] arr = { 9, 8, 3, 5, 2 ,12,7,25,15};
            Console.Write("冒泡排序前:" + "".PadRight(5));
            PrintArray(arr); //打印数组元素
            SelectSort(arr); //调用排序方法
            Console.Write("冒泡排序后:");
            PrintArray(arr); //打印数组元素


            Console.ReadKey();
        }
        //定义打印数组的方法
        public static void PrintArray(int[] arr)
        {
            //循环遍历数组的元素
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i] + " "); //打印元素和空格
            }
            Console.WriteLine();
        }
        //定义对数组排序的方法
        public static void BubbleSort(int[] arr)
        {
            //定义外层循环
            for (int i = 0; i < arr.Length - 1; i++)
            {
                //定义内层循环
                for (int j = 0; j < arr.Length - i - 1; j++)
                {
                    //Console.WriteLine(arr.Length - i);
                    if (arr[j] > arr[j + 1]) //比较相邻元素
                    {
                        //下面的三行代码用于交换两个元素
                        int temp = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j + 1] = temp;
                    }
                }
                Console.Write("第" + (i + 1) + "轮排序后:");
                PrintArray(arr); //每轮比较结束打印数组元素
            }
        }

        //定义对数组排序的方法
        public static void SelectSort(int[] arr)
        {
            //定义外层循环
            for (int i = 0; i < arr.Length - 1; i++)
            {
                //定义内层循环
                for (int j = i + 1; j <= arr.Length - 1; j++)
                {
                    //Console.WriteLine(arr.Length - i);
                    if (arr[i] > arr[j ]) //比较相邻元素
                    {
                        //下面的三行代码用于交换两个元素
                        int temp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = temp;
                    }
                }
                Console.Write("第" + (i + 1) + "轮排序后:");
                PrintArray(arr); //每轮比较结束打印数组元素
            }
        }
    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySchool namespace MySchool { partial class FrmLogin { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmLogin)); this.btnLogin = new System.Windows.Forms.Button(); this.btnCancel = new System.Windows.Forms.Button(); this.txtUserName = new System.Windows.Forms.TextBox(); this.txtPwd = new System.Windows.Forms.TextBox(); this.cboLoginType = new System.Windows.Forms.ComboBox(); this.lblLoginId = new System.Windows.Forms.Label(); this.lblPwd = new System.Windows.Forms.Label(); this.lblLoginType = new System.Windows.Forms.Label(); this.SuspendLayout(); // // btnLogin // this.btnLogin.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; this.btnLogin.BackColor = System.Drawing.Color.Transparent; this.btnLogin.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnLogin.BackgroundImage"))); this.btnLogin.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None; this.btnLogin.Cursor = System.Windows.Forms.Cursors.Hand; this.btnLogin.FlatAppearance.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(128))))); this.btnLogin.FlatAppearance.BorderSize = 0; this.btnLogin.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.btnLogin.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.btnLogin.Location = new System.Drawing.Point(181, 299); this.btnLogin.Name = "btnLogin"; this.btnLogin.Size = new System.Drawing.Size(84, 21); this.btnLogin.TabIndex = 3; this.btnLogin.Text = "登 录"; this.btnLogin.UseVisualStyleBackColor = false; // // btnCancel // this.btnCancel.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnCancel.BackgroundImage"))); this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel; this.btnCancel.FlatAppearance.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(128))))); this.btnCancel.FlatAppearance.BorderSize = 0; this.btnCancel.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.btnCancel.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.btnCancel.Location = new System.Drawing.Point(283, 299); this.btnCancel.Name = "btnCancel"; this.btnCancel.Size = new System.Drawing.Size(84, 21); this.btnCancel.TabIndex = 4; this.btnCancel.Text = "取 消"; this.btnCancel.UseVisualStyleBackColor = true; // // txtUserName // this.txtUserName.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.txtUserName.Location = new System.Drawing.Point(254, 190); this.txtUserName.Name = "txtUserName"; this.txtUserName.Size = new System.Drawing.Size(167, 23); this.txtUserName.TabIndex = 0; // // txtPwd // this.txtPwd.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.txtPwd.Location = new System.Drawing.Point(254, 227); this.txtPwd.Name = "txtPwd"; this.txtPwd.Size = new System.Drawing.Size(167, 23); this.txtPwd.TabIndex = 1; this.txtPwd.UseSystemPasswordChar = true; // // cboLoginType // this.cboLoginType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cboLoginType.Items.AddRange(new object[] { "系统管理员", "学生"}); this.cboLoginType.Location = new System.Drawing.Point(254, 264); this.cboLoginType.Name = "cboLoginType"; this.cboLoginType.Size = new System.Drawing.Size(167, 22); this.cboLoginType.TabIndex = 2; // // lblLoginId // this.lblLoginId.AutoSize = true; this.lblLoginId.BackColor = System.Drawing.Color.Transparent; this.lblLoginId.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.lblLoginId.Location = new System.Drawing.Point(181, 194); this.lblLoginId.Name = "lblLoginId"; this.lblLoginId.Size = new System.Drawing.Size(67, 14); this.lblLoginId.TabIndex = 5; this.lblLoginId.Text = "用户名:"; // // lblPwd // this.lblPwd.AutoSize = true; this.lblPwd.BackColor = System.Drawing.Color.Transparent; this.lblPwd.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.lblPwd.Location = new System.Drawing.Point(181, 231); this.lblPwd.Name = "lblPwd"; this.lblPwd.Size = new System.Drawing.Size(68, 14); this.lblPwd.TabIndex = 6; this.lblPwd.Text = "密 码:"; // // lblLoginType // this.lblLoginType.AutoSize = true; this.lblLoginType.BackColor = System.Drawing.Color.Transparent; this.lblLoginType.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.lblLoginType.Location = new System.Drawing.Point(181, 268); this.lblLoginType.Name = "lblLoginType"; this.lblLoginType.Size = new System.Drawing.Size(82, 14); this.lblLoginType.TabIndex = 7; this.lblLoginType.Text = "登录类型:"; // // FrmLogin // this.AcceptButton = this.btnLogin; this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 14F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage"))); this.CancelButton = this.btnCancel; this.ClientSize = new System.Drawing.Size(506, 372); this.Controls.Add(this.cboLoginType); this.Controls.Add(this.lblLoginType); this.Controls.Add(this.lblPwd); this.Controls.Add(this.lblLoginId); this.Controls.Add(this.txtPwd); this.Controls.Add(this.txtUserName); this.Controls.Add(this.btnCancel); this.Controls.Add(this.btnLogin); this.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.MaximizeBox = false; this.Name = "FrmLogin"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "登录"; this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Button btnLogin; private System.Windows.Forms.Button btnCancel; private System.Windows.Forms.TextBox txtUserName; private System.Windows.Forms.TextBox txtPwd; private System.Windows.Forms.ComboBox cboLoginType; private System.Windows.Forms.Label lblLoginId; private System.Windows.Forms.Label lblPwd; private System.Windows.Forms.Label lblLoginType; } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值