Vb编写程序以打印2和3的倍数

时间:2014-06-13 21:20:58

标签: vb.net

对于我的班级,我需要编写一个程序来查找2和3的倍数。我得到的代码会得到我输入程序的任何数字的倍数。我的问题是,我创建的消息框中没有显示任何内容,我也不知道原因。这是代码。

Public Class form1
    Private Sub Button1_Click(ByVal Sender)
        Dim Number1 As Integer
        Dim Number2 As Integer
        Dim Multiplier As Integer
        Dim Answer As Integer
        Dim i As Integer
        Number1 = Val(TextBox1.Text)
        Number2 = Val(TextBox2.Text)
        Multiplier = 1
        Do While Multiplier <= 10
            For i = Number1 To Number2
                Answer = i * Multiplier
                ListBox1.Items.Add(i & "*" & Multiplier & "=" & Answer)
            Next i
            Multiplier = Multiplier + 1
        Loop
    End Sub
End Class

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:0)

我还没有测试过,但我认为,这就是你要找的东西 - 所有数字都可以用文本框中数字范围内的数字1到10的乘数除以3和2。在您的代码中,我不知道您在哪里清除可以除以2和3的数字

Private Sub Button1_Click(ByVal sender as Object, ByVal e as EventArgs) Handles Button1.Click
    Dim num1 As Integer = Integer.Parse(TextBox1.Text)
    Dim num2 As Integer = Integer.Parse(TextBox2.Text)
    ' may be need to check num2 > num1

    Dim sum As Integer       
    For mult as Integer = 1 to 10
        For i as integer = num1 To num2
            total = i * mult
            If sum Mod 2 = 0 OrElse sum Mod 3 = 0 Then 
                ListBox1.Items.Add(i.ToString() & " * " & mult & " = " & sum.ToString())
            End If
        Next i

    Next
End Sub

答案 1 :(得分:0)

这是我对你想要的最好的猜测。你说你想要给节目的任何数字都想要2和3的倍数,这就是它的作用。如果你想要其他任何东西的倍数,只需添加到系数声明中{}内的部分。我建议使用NumericUpDown而不是使用文本框进行输入; GUI将对最终用户更直观。

Imports System.Text
Public Class Form1


    Private Property maxNumb As Integer
    Private Property minNumb As Integer
    Private coefficients() As Integer = {2, 3}

    Private Sub Button1_Click(sender As Button, e As EventArgs) Handles Button1.Click
        Dim sb As New StringBuilder
        For i = Me.minNumb To maxNumb Step 1
            For Each coef As Integer In coefficients
                sb.Append(i & " * ").Append(coef).Append(" = ").Append(i * coef)
                Me.ListBox1.Items.Add(sb.ToString)
                sb.Clear()
            Next
        Next
        Me.ListBox1.Refresh()
    End Sub

    Private Sub NumericUpDown_ValueChanged(sender As NumericUpDown, e As EventArgs) Handles min_NumericUpDown1.ValueChanged, max_NumericUpDown2.ValueChanged
        If sender.Name.Contains("max") Then
            Me.maxNumb = sender.Value
        Else
            Me.minNumb = sender.Value
        End If
    End Sub
End Class