324847373 发表于 2021-9-29 14:12:36

麻烦哥哥教我写c#

本帖最后由 324847373 于 2021-9-29 14:47 编辑

如图,怎么精简代码,让六个变更为一个,不让代码太冗余。
求求各位gege了。6个label,1个textbox,1个按钮,1个计时器。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Win_keydown2
{
    public partial class Form1 : Form
    {
      public Form1()
      {
            InitializeComponent();
      }
      int ystep;
      private void button1_Click(object sender, EventArgs e)
      {
            this.timer1.Enabled = !this.timer1.Enabled;
            if (this.timer1.Enabled == true)
                this.button1.Text = "暂停";
            else
                this.button1.Text = "开始";
            this.textBox1.Focus();//让textbox1获得焦点。
      }

      private void Form1_Load(object sender, EventArgs e)
      {
            ystep = 10;
      }

      private void timer1_Tick(object sender, EventArgs e)
      {
            this.label1.Top += ystep; this.label2.Top += ystep; this.label3.Top += ystep;
            this.label4.Top += ystep; this.label5.Top += ystep; this.label6.Top += ystep;
            //填写输了的代码。
            if ((this.label1.Bottom > this.textBox1.Top)||(this.label2.Bottom > this.textBox1.Top)
                ||(this.label3.Bottom > this.textBox1.Top)||(this.label4.Bottom > this.textBox1.Top)
                || (this.label5.Bottom > this.textBox1.Top) || (this.label6.Bottom > this.textBox1.Top))
            {
                this.timer1.Enabled = false;
                if (MessageBox.Show("您输了是否继续?", "提示", MessageBoxButtons.YesNo)
                  == DialogResult.Yes)
                { this.timer1.Enabled = true;
                  init1(); init2();
                  init3(); init4();
                  init5(); init6();
                }
                else
                  this.Close();//结束程序的运行
            }
      }
      private void init1()
      {
            Random r = new Random();//定义r属于随机类的一个变量,r的值[0,1)
            int t = r.Next(26);   //定义变量t是随机数,范围在[0,26)
            char ch = Convert.ToChar(97 + t);//Convert.ToChar按照ASCII码将数字转换为字符。
            this.label1.Text = ch.ToString(); //将字符转换为字符串。
            this.label1.Top = 20;
            ystep++;
            this.textBox1.Focus();
      }
      private void init2()
      {
            Random r = new Random();//定义r属于随机类的一个变量,r的值[0,1)
            int t = r.Next(26);   //定义变量t是随机数,范围在[0,26)
            char ch = Convert.ToChar(97 + t);//Convert.ToChar按照ASCII码将数字转换为字符。
            this.label2.Text = ch.ToString(); //将字符转换为字符串。
            this.label2.Top = 20;
            ystep++;
            this.textBox1.Focus();
      }
      private void init3()
      {
            Random r = new Random();//定义r属于随机类的一个变量,r的值[0,1)
            int t = r.Next(26);   //定义变量t是随机数,范围在[0,26)
            char ch = Convert.ToChar(97 + t);//Convert.ToChar按照ASCII码将数字转换为字符。
            this.label3.Text = ch.ToString(); //将字符转换为字符串。
            this.label3.Top = 20;
            ystep++;
            this.textBox1.Focus();
      }
      private void init4()
      {
            Random r = new Random();//定义r属于随机类的一个变量,r的值[0,1)
            int t = r.Next(26);   //定义变量t是随机数,范围在[0,26)
            char ch = Convert.ToChar(97 + t);//Convert.ToChar按照ASCII码将数字转换为字符。
            this.label4.Text = ch.ToString(); //将字符转换为字符串。
            this.label4.Top = 20;
            ystep++;
            this.textBox1.Focus();
      }
      private void init5()
      {
            Random r = new Random();//定义r属于随机类的一个变量,r的值[0,1)
            int t = r.Next(26);   //定义变量t是随机数,范围在[0,26)
            char ch = Convert.ToChar(97 + t);//Convert.ToChar按照ASCII码将数字转换为字符。
            this.label5.Text = ch.ToString(); //将字符转换为字符串。
            this.label5.Top = 20;
            ystep++;
            this.textBox1.Focus();
      }
      private void init6()
      {
            Random r = new Random();//定义r属于随机类的一个变量,r的值[0,1)
            int t = r.Next(26);   //定义变量t是随机数,范围在[0,26)
            char ch = Convert.ToChar(97 + t);//Convert.ToChar按照ASCII码将数字转换为字符。
            this.label6.Text = ch.ToString(); //将字符转换为字符串。
            this.label6.Top = 20;
            ystep++;
            this.textBox1.Focus();
      }

      private void textBox1_TextChanged(object sender, EventArgs e)
      {
            if (this.textBox1.Text == this.label1.Text)
                init1();
            if(this.textBox1.Text == this.label2.Text)
                init2();
            if(this.textBox1.Text == this.label3.Text)
                init3();
            if(this.textBox1.Text == this.label4.Text)
                init4();
            if(this.textBox1.Text == this.label5.Text)
                init5();
            if(this.textBox1.Text == this.label6.Text)
                init6();
            this.textBox1.Clear();
      }
    }
}




cxxjackie 发表于 2021-9-29 14:12:37

C#以前学过一点,忘的差不多了,不过语言应该是相通的吧,我不懂装懂一下{:4_100:}
init1到init6的逻辑是一样的,只是label不一样,那使用同一个函数,将label作为参数传入不就可以了吗?像这样:
private void init(Label label) //类型可能不对,我猜的
{
Random r = new Random();//定义r属于随机类的一个变量,r的值[0,1)
int t = r.Next(26);   //定义变量t是随机数,范围在[0,26)
char ch = Convert.ToChar(97 + t);//Convert.ToChar按照ASCII码将数字转换为字符。
label.Text = ch.ToString(); //将字符转换为字符串。
label.Top = 20;
ystep++;
this.textBox1.Focus();
}

王一之 发表于 2021-9-29 14:19:38

优化空气,至少发伪代码吧

324847373 发表于 2021-9-29 14:38:11

王一之 发表于 2021-9-29 14:19
优化空气,至少发伪代码吧

我发了截图,图没了。。。

王一之 发表于 2021-9-29 15:34:20

324847373 发表于 2021-9-29 14:38
我发了截图,图没了。。。

好吧。。。图片上传流程是有点麻烦 等大佬来解答,不搞C#

李恒道 发表于 2021-9-29 16:19:29

这论坛可能都没人会...呜呜呜
前端论坛,哈哈哈

324847373 发表于 2021-9-29 16:39:11

麻了,gege们都不会{:4_115:}

324847373 发表于 2021-9-29 22:29:33

cxxjackie 发表于 2021-9-29 20:15
C#以前学过一点,忘的差不多了,不过语言应该是相通的吧,我不懂装懂一下
init1到init6的逻辑是一 ...

请问那6个init();应该怎么改呢?

cxxjackie 发表于 2021-9-29 22:52:22

324847373 发表于 2021-9-29 22:29
请问那6个init();应该怎么改呢?

init(this.label1);
init(this.label2);

324847373 发表于 2021-9-29 22:53:33

cxxjackie 发表于 2021-9-29 20:15
C#以前学过一点,忘的差不多了,不过语言应该是相通的吧,我不懂装懂一下
init1到init6的逻辑是一 ...

!(data/attachment/forum/202109/29/225225jzu2dfs1soc2roq0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300 "image.png")

哥哥红框里面的代码还可以再精简吗?
页: [1] 2
查看完整版本: 麻烦哥哥教我写c#