用c# 编写 初始化:生成一个大小为n的数组,随机初始化其中[1, n]个数字,并按数字升序排列图形并显示。 专注力训练:生成一个大小为m*m的矩阵,用随机图片填满方阵中的每个位置。每个图片下方设置一个文本框,用户可以输入数字。计时开始,并显示所用时间。当用户点击结束按钮时,计时结束,显示本次用时和正确率(正确标准:用户输入数字与初始化时图片对应数字相同)。 历史记录:使用数据库或文件记录每个n和m下的最好成绩。查询不同n和m时,查询历史最好成绩。查询某一n和某一m时,查询该n和m下所有的历史成绩。

时间: 2024-02-03 16:14:04 浏览: 25
以下是用 C# 编写的示例代码,实现了上述三个功能: 1. 生成并显示初始化数组 ``` static void Main(string[] args) { int n = 10; int[] arr = new int[n]; Random random = new Random(); int numCount = random.Next(1, n+1); for (int i = 0; i < numCount; i++) { int num = random.Next(1, n+1); while (Array.IndexOf(arr, num) >= 0) { num = random.Next(1, n+1); } arr[i] = num; } Array.Sort(arr); foreach (int num in arr) { Console.Write(num + " "); } } ``` 2. 生成并显示矩阵,并计时用户输入和正确率 ``` static void Main(string[] args) { int m = 5; Bitmap[] images = new Bitmap[m * m]; for (int i = 0; i < m * m; i++) { images[i] = new Bitmap("image" + i + ".jpg"); } int correctCount = 0; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); for (int i = 0; i < m * m; i++) { PictureBox pictureBox = new PictureBox(); pictureBox.Image = images[i]; pictureBox.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox.BorderStyle = BorderStyle.FixedSingle; TextBox textBox = new TextBox(); textBox.Width = pictureBox.Width; textBox.Top = pictureBox.Bottom; textBox.KeyPress += (sender, e) => { if (e.KeyChar == (char)Keys.Enter) { int inputNum; if (int.TryParse(textBox.Text, out inputNum)) { if (inputNum == i + 1) { correctCount++; } } pictureBox.Enabled = false; textBox.Enabled = false; if (correctCount == m * m) { stopwatch.Stop(); MessageBox.Show("用时:" + stopwatch.ElapsedMilliseconds / 1000.0 + "秒,正确率:" + (correctCount * 100.0 / (m * m)) + "%"); } } }; pictureBox.Controls.Add(textBox); pictureBox.Left = (i % m) * pictureBox.Width; pictureBox.Top = (i / m) * pictureBox.Height; pictureBox.Parent = this; } } ``` 3. 使用 SQLite 数据库记录历史成绩 ``` static void Main(string[] args) { string dbPath = "history.sqlite"; if (!File.Exists(dbPath)) { SQLiteConnection.CreateFile(dbPath); using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + dbPath)) { connection.Open(); string createTableSql = "CREATE TABLE history(id INTEGER PRIMARY KEY AUTOINCREMENT, n INT, m INT, time FLOAT, correctRate FLOAT)"; SQLiteCommand command = new SQLiteCommand(createTableSql, connection); command.ExecuteNonQuery(); } } using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + dbPath)) { connection.Open(); int n = 10; int m = 5; float time = 10.0f; float correctRate = 80.0f; string insertSql = "INSERT INTO history(n, m, time, correctRate) VALUES(@n, @m, @time, @correctRate)"; SQLiteCommand command = new SQLiteCommand(insertSql, connection); command.Parameters.AddWithValue("@n", n); command.Parameters.AddWithValue("@m", m); command.Parameters.AddWithValue("@time", time); command.Parameters.AddWithValue("@correctRate", correctRate); command.ExecuteNonQuery(); string querySql1 = "SELECT MAX(correctRate) FROM history WHERE n=@n AND m=@m"; SQLiteCommand command1 = new SQLiteCommand(querySql1, connection); command1.Parameters.AddWithValue("@n", n); command1.Parameters.AddWithValue("@m", m); object maxCorrectRate1 = command1.ExecuteScalar(); Console.WriteLine("n=" + n + ", m=" + m + "的最好成绩(根据正确率):" + maxCorrectRate1); string querySql2 = "SELECT * FROM history WHERE n=@n AND m=@m ORDER BY correctRate DESC"; SQLiteCommand command2 = new SQLiteCommand(querySql2, connection); command2.Parameters.AddWithValue("@n", n); command2.Parameters.AddWithValue("@m", m); SQLiteDataReader reader = command2.ExecuteReader(); Console.WriteLine("n=" + n + ", m=" + m + "的所有历史成绩:"); while (reader.Read()) { Console.WriteLine("id=" + reader.GetInt32(0) + ", time=" + reader.GetFloat(3) + ", correctRate=" + reader.GetFloat(4)); } } } ```

相关推荐

最新推荐

recommend-type

C#初始化数组的方法小结

主要介绍了C#初始化数组的方法,总结分析了C#声明与初始化一维数组及多维数组的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
recommend-type

C#中数组初始化与数组元素复制的方法

主要介绍了C#中数组初始化与数组元素复制的方法,涉及C#中数组的创建、初始化及使用Array.Copy方法复制数组元素的技巧,需要的朋友可以参考下
recommend-type

C#中结构(struct)的部分初始化和完全初始化实例分析

主要介绍了C#中结构(struct)的部分初始化和完全初始化,通过实例分析了结构初始化中常见的错误及技巧,有助于加深对C#结构(struct)的认识,需要的朋友可以参考下
recommend-type

C#实现输入10个数存入到数组中并求max和min及平均数的方法示例

主要介绍了C#实现输入10个数存入到数组中并求max和min及平均数的方法,涉及C#简单数据转换与数值运算相关操作技巧,需要的朋友可以参考下
recommend-type

C#字符串数组转换为整形数组的方法

主要介绍了C#字符串数组转换为整形数组的方法,涉及C#数组遍历与转换的相关技巧,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】增量式PID的simulink仿真实现

# 2.1 Simulink仿真环境简介 Simulink是MATLAB中用于建模、仿真和分析动态系统的图形化环境。它提供了一个直观的用户界面,允许用户使用块和连接线来创建系统模型。Simulink模型由以下元素组成: - **子系统:**将复杂系统分解成更小的、可管理的模块。 - **块:**代表系统中的组件,如传感器、执行器和控制器。 - **连接线:**表示信号在块之间的流动。 Simulink仿真环境提供了广泛的块库,涵盖了各种工程学科,包括控制系统、电子和机械工程。它还支持用户自定义块的创建,以满足特定仿真需求。 # 2. Simulink仿真环境的搭建和建模 ### 2.
recommend-type

训练集和测试集的准确率都99%,但是预测效果不好

即使训练集和测试集的准确率都很高,但是在实际的预测中表现不佳,可能会有以下几个原因: 1. 数据质量问题:模型训练和测试的数据可能存在问题,比如数据标签错误、数据集采样不均衡、数据集中存在异常值等问题,这些问题可能会导致模型在实际预测中表现不佳。 2. 特征工程问题:模型的特征工程可能存在问题,导致模型无法很好地捕捉数据的特征,从而影响预测效果。 3. 模型选择问题:模型的选择可能不合适,比如选择的模型太简单,无法很好地拟合数据,或者选择的模型太复杂,导致过拟合等问题。 4. 超参数调整问题:模型的超参数可能没有调整到最佳状态,需要进行调整。 针对以上可能的原因,可以采取相应的措施进
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。