English 中文(简体)
视力基础的预期价值(历史期望)。 NET
原标题:Expected Value (mathematical expectation) in Visual Basic .NET
  • 时间:2011-11-21 14:59:45
  •  标签:
  • vb.net
  • math

I ve got a program which starts with welcome screen "Enter the number of the random variables X" and this number refers to the columns. If it is entered 5 => a DataGridView will be created with 5 columns and 2 rows (first one is for random variable entered by the user, and the second is for probability). For example:

X 1 2 3 4 5 6

p 2 2 2 2 2 2

页: 1 So EX = x1*p1 + x2*p2 +... 这里的问题是:如何乘数,然后添加?

There are only 2 rows, that s how much I need :

I ve enabled only 2 rows, that s how much I must have for this program:

  Const allowedRows As Integer = 2

    Private Sub DataGridView1_UserAddedRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles DataGridView1.UserAddedRow
        DataGridView1.AllowUserToAddRows = DataGridView1.RowCount <= allowedRows
    End Sub

    Private Sub DataGridView1_UserDeletedRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles DataGridView1.UserDeletedRow
        DataGridView1.AllowUserToAddRows = DataGridView1.RowCount <= allowedRows
    End Sub

The loop is:

    For j = 0 To DataGridView1.Columns.Count
    For i As Integer = 0 To DataGridView1.RowCount - 1
        multi = CInt(DataGridView1.Rows(i).Cells(j).Value) * CInt(DataGridView1.Rows(i).Cells(j).Value)
    Next
    EX += multi
Next

我认为,它必须如此(它给OfRange例外),但我为什么在这里写字,这样,如果任何人知道如何做,我就非常感激。 提前感谢。

最佳回答

我更熟悉Charp,这样,如果它不达到100%,就可宽恕我的辛子。

我感觉到,你走的路最多。 如果我的VB syntax是......要么是正确的话,那么我就给这两套法典打下基础。

VB

For j = 0 To DataGridView1.Columns.Count          
   EX += CInt(DataGridView1.Rows(0).Cells(j).Value) * // mult p*X in each column
      CInt(DataGridView1.Rows(1).Cells(j).Value)      //  and add total  EX            
Next   

C#

//Do math
int EX = 0;
for (int j = 0; j < dgv.Columns.Count; j++)
{
   EX += Convert.ToInt32(dgv.Rows[0].Cells[j].Value) *
      Convert.ToInt32(dgv.Rows[1].Cells[j].Value);
}
问题回答

暂无回答




相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签