2017-02-24 61 views
-1

如果我輸入5,它將產生一個5(5x5) 的方陣。我該如何乘以1,7,13,19,25? How do I multiply 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; 
     } 
    } 
} 
+0

上[綁定]讀取向上(https://msdn.microsoft.com/en -us /庫/ ef2xyb33(v = vs.110)的.aspx)。然後你可以乘以文本框綁定的數組。 –

回答

0

如果你正在尋找一種算法:

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); 
    } 
0

從零隻是循環到矩陣的大小。該指數將被用來獲得每個對角值(此代碼假定每個文本框包含的整數,並且是不是空白):

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