【问题标题】:How do i access these arrays so that i can multiply inputted values in the diagonal?我如何访问这些数组,以便我可以在对角线上乘以输入值?
【发布时间】:2017-02-24 16:57:36
【问题描述】:

如果我输入 5,它将生成一个 5 (5x5) 的方阵 如何乘以 1、7、13、19、25?

我的代码是否有任何适用的算法,所以我可以乘以对角线还是需要重写一个新的?

public partial class Form1 : Form {
    public int e = 0;
    int Row = 0;
    int Column = 0;
    int YAxisPosition = 0; 
    int XAxisPosition = 0;
    int Counter = 0;
    int PositionalValue = 0; 
    TextBox[] MyTextBoxDimA = new TextBox[999999]; 
    TextBox tbRow = new TextBox();
    Button MyButton = new Button();

    public Form1() {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e) {
        AutoScroll = true;
        WindowState = System.Windows.Forms.FormWindowState.Maximized;

        //GENERATING THE SIZE BUTTON
        tbRow = new TextBox();
        tbRow.Text = "5";
        tbRow.Size = new Size(100, 10);
        tbRow.Location = new Point(0, 0);
        Controls.Add(tbRow);

        //GENERATE MATRIX BUTTON
        MyButton = new Button();
        MyButton.Text = "GENERATE MATRIX";
        MyButton.Size = new Size(200, 25);
        MyButton.Click += new EventHandler(MyButton_Click);
        MyButton.Location = new Point(0, 30);
        Controls.Add(MyButton);
    }

    public void MyButton_Click(object sender, EventArgs ee) {
        //CODE FOR GENERATING MATRIX A
        e = 1;
        PositionalValue = 1;
        Counter = 1;

        //POSITION
        YAxisPosition = 60;
        XAxisPosition = 0;
        Row = Convert.ToInt32(tbRow.Text);
        Column = Convert.ToInt32(tbRow.Text);
        while (Row >= e) {
            while (Column >= Counter) {
                MyTextBoxDimA[PositionalValue] = new TextBox(); 
                MyTextBoxDimA[PositionalValue].Location =
                    new Point(XAxisPosition, YAxisPosition); //coordinates (start)
                MyTextBoxDimA[PositionalValue].Size = new Size(70, 10); 
                MyTextBoxDimA[PositionalValue].Text = Convert.ToString(PositionalValue);
                Controls.Add(MyTextBoxDimA[PositionalValue]);
                XAxisPosition = XAxisPosition + 100;
                PositionalValue++;
                Counter++;
            }
            YAxisPosition = YAxisPosition + 50;
            Counter = 1; 
            e++;
            XAxisPosition = 0;
        }
    }
}

【问题讨论】:

  • 阅读databinding。然后你可以将文本框绑定到的数组相乘。

标签: c# windows forms


【解决方案1】:

如果您正在寻找算法:

    static void Main(string[] args)
    {
        int n = 5;
        int ans = 1;
        int current = 1;
        for (int i = 1; i <= n; i++)
        {
            ans = ans * current;
            current += n + 1;
        }
        Console.WriteLine(ans);
    }

【讨论】:

    【解决方案2】:

    只需从零循环到矩阵的大小。该索引将用于获取每个对角线值(此代码假定每个文本框都包含一个整数且不为空):

    int matrixSize = 5;
    int product = 1;
    for (int i = 0; i < matrixSize; i++)
    {
        product *= int.Parse(MyTextBoxDimA[i,i].Text);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-27
      相关资源
      最近更新 更多