Vb.net 乘法图

Vb.net 乘法图,vb.net,Vb.net,我正在尝试制作一个像图中那样的乘法图,我对编码相当陌生,甚至不知道从哪里开始。到目前为止,我已经在输出中包含了一个图像,以及下面的代码。 任何帮助都将不胜感激 公共类frmL24 私有子btnCreateChart\u单击(发件人作为对象,e作为事件参数)处理btnCreateChart。单击 尺寸j为整数=1 尺寸i为整数=1 而(j开始逐段构建。下面是一个循环的算法,该循环的计数范围为1到10 'Start with a variable inited to 1 'while that v

我正在尝试制作一个像图中那样的乘法图,我对编码相当陌生,甚至不知道从哪里开始。到目前为止,我已经在输出中包含了一个图像,以及下面的代码。 任何帮助都将不胜感激

公共类frmL24
私有子btnCreateChart\u单击(发件人作为对象,e作为事件参数)处理btnCreateChart。单击
尺寸j为整数=1
尺寸i为整数=1

而(j开始逐段构建。下面是一个循环的算法,该循环的计数范围为1到10

'Start with a variable inited to 1
'while that variable is less than or equal to 10
  'print out the variable (by print I mean "add it onto the end of a textbox text")
  'increment the variable
'loop
这里有一个算法可以打印出一个时间表:

'have a variable for the times table we're doing, start with the 1 times table, call the variable "timestable"

'Start with a variable inited to 1, call it "number"
'while "number" is less than or equal to 10
  'print out the result of "number" multiplied by "timestable"
  'increment the value of "number"
'loop
希望你能看到这只是第一个算法的一个小小的改进。你把timestable设置为1,它会打印出和第一个相同的东西。你把它设置为2,它会打印出2个timestable等等

现在再次修改algo,这次使用循环将timestable变量更改十次:

'have a variable for the times table we're doing, start with the 1 times table, call the variable "timestable"

'while "timestable" is less than or equal to 10

  'Start with a variable inited to 1, call it "number"
  'while "number" is less than or equal to 10
    'print out the result of "number" multiplied by "timestable"
    'increment the value of "number"
  'loop

  'increment the "timestable" variable
'loop
现在将打印出从1到10的时间表

'Start with a variable inited to 1
'while that variable is less than or equal to 10
  'print out the variable (by print I mean "add it onto the end of a textbox text")
  'increment the variable
'loop
您可能没有需要处理的只是一些格式化问题。如果您确实遵循了这些说明,那么您的输出将如下所示:

12345678910
2468101214161820
您可以在以下内容之间添加空格:

1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
但仍然没有很好地对齐,是吗?我们可以使用一些逻辑,因为这有点过于简单:

'print out the result of "number" multiplied by "timestable"
也许这会更好:

'calculate the result of "number" multiplied by "timestable"
'if the result is less than 10, print 2 spaces then the result
'else print 1 space then the result
突然我们得到:

  1  2  3  4  5  6  7  8  9 10
  2  4  6  8 10 12 14 16 18 20
然后你只需要在打印标题等周围添加一些绒毛


你会注意到我没有发布任何代码,因为这是你的家庭作业,不是我的。我知道怎么做,但如果我为你做,它会剥夺你自己学习这个过程的机会。你的最终结果绝对应该有一堆评论,就像我在这里写的评论一样(你理解的算法版本),下面有代码。即使你从来没有按照规范要求工作过,你也应该交上代码和注释,这样讲师就可以看到你的想法哪里出了问题(注释中的算法是错误的),或者你的想法哪里是对的,但你的答案(你编写的代码)是错误的。这就像“展示你的工作”在数学考试中,使用错误的循环类型是没有意义的。请向老师解释一下

使用错误类型的控件来显示数据也是毫无意义的。
ListBox
用于列表。数据更像一个表而不是列表。
DataGridView
用于显示表格数据。将
DataGridView
添加到表单中

DataTable
是.Net Framework中的一个类,它是表(列和行)的内存表示形式。您可以将其填充到内存中,然后将其用作
DataGridView
数据源来显示数据。这比在添加数据时不断更改显示更有效

这个练习应该是对
DataTable
的一个很好的介绍,它是开始数据库工作时非常有用的工具

默认情况下,For
循环将
i
(或您命名的任何变量)递增1

首先,我们创建一个
DataTable
,就像其他变量一样,只使用
New
关键字,因为我们正在创建一个类的实例。接下来添加您需要的列。第一列是X,然后是数字1到10

现在添加数据(行)。每行的第一列和第二列只是
i
。第三列是
i
x2,依此类推

最后,我们将
DataGridView
DataSource
属性设置为我们填充的
DataTable
dt
),并对其进行一些整理,使其看起来更美观。有许多属性可以自定义
DataGridView
的外观

Private Sub OPCode()
    Dim dt As New DataTable
    dt.Columns.Add("X", GetType(Integer))
    For i = 1 To 10
        dt.Columns.Add(i.ToString, GetType(Integer))
    Next
    For i = 1 To 10
        dt.Rows.Add(i, i, i * 2, i * 3, i * 4, i * 5, i * 6, i * 7, i * 8, i * 9, i * 10)
    Next
    DataGridView1.DataSource = dt
    DataGridView1.AutoResizeColumns()
    DataGridView1.AutoResizeRows()
    'To make the numbers line up nicely
    For Each col As DataGridViewColumn In DataGridView1.Columns
        col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
    Next
End Sub

请参见:和。如果您在完成所有这些之后仍然存在问题,那么这可能会很有用,我将非常乐意提供帮助,因为这样您可以将此问题的格式设置为更好、更吸引人的帮助方式:3您必须改变您对此的看法。在您向我们展示的图片中,数字是(可能是)与它们的行/列标题无关,但以格式化字符串的形式写入,因此它们看起来像网格。您必须确定如何写入此网格的一行,然后自动执行此逻辑,以便可以在所有需要的行上循环它(当然,现在我看到此实验室是关于嵌套循环的…这确实是个想法!)。你必须在循环时使用
吗?这似乎是
循环的一个明显的候选。是的,我做了,我已经更新了我现在所拥有的,但我仍然很不正确。我没有循环方面的经验。@CaiusJard你可能是对的,但也许我关于数据表是什么的一段总结会引起一些好奇。