none
Help fixing my multiplication table RRS feed

  • Question

  • Not sure what I'm doing wrong with my table but it seems my equation is slightly off. I'm still new to this so i'm not really good at using debugger mode to help me solve this yet.

    Public Class frmMain
    
        Private Sub btnDoLoop_Click(sender As Object, e As EventArgs) Handles btnDoLoop.Click
            ' Displays a multiplication table.
            Dim intNum As Integer
            Dim intNum1 As Integer = 1
            Dim intNum2 As Integer
    
            Integer.TryParse(txtNumber.Text, intNum)
    
            Do While intNum1 <= 9
                intNum2 = intNum * intNum1
                intNum1 += 1
                lblTable.Text = lblTable.Text &
                intNum.ToString & " * " &
                intNum1.ToString & " = " &
                intNum2.ToString & "    " & ControlChars.NewLine
            Loop
        End Sub

    Everything displays properly but the results are slightly off.

    I intended it for it to end a * 9 and for some reason it's not showing * 1 so the results are off lol.

    Thursday, February 21, 2019 11:26 PM

Answers

  •  You should be starting your (intNum1) variable at 0 instead of 1.  Then you should not be adding 1 to (intNum1) until the last line in your While loop.

            ' Displays a multiplication table.
            Dim intNum As Integer
            Dim intNum1 As Integer = 0 'start at 0, not 1
            Dim intNum2 As Integer
    
            Integer.TryParse(txtNumber.Text, intNum)
    
            Do While intNum1 <= 9
                intNum2 = intNum * intNum1
                lblTable.Text = lblTable.Text &
                intNum.ToString & " * " &
                intNum1.ToString & " = " &
                intNum2.ToString & "    " & ControlChars.NewLine
                intNum1 += 1 'before continuing the loop, add 1 to intNum1
            Loop
    
     

     


    If you say it can`t be done then i`ll try it

    • Marked as answer by Godren Friday, February 22, 2019 12:00 AM
    Thursday, February 21, 2019 11:52 PM

All replies

  • Hi

    Here is one way.

      Private Sub btnDoLoop_Click(sender As Object, e As EventArgs) Handles btnDoLoop.Click
        If TextBox1.Text = Nothing Then Exit Sub
        Dim int1 As Integer = CInt(Trim(TextBox1.Text))
        If int1 > 1 AndAlso int1 < 10 Then
          For i As Integer = 1 To 9
            ListBox1.Items.Add(TextBox1.Text & " x " & i.ToString & " = " & (i * int1).ToString)
          Next
        Else
          TextBox1.Text = Nothing
        End If
      End Sub


    Regards Les, Livingston, Scotland

    Thursday, February 21, 2019 11:36 PM
  •  You should be starting your (intNum1) variable at 0 instead of 1.  Then you should not be adding 1 to (intNum1) until the last line in your While loop.

            ' Displays a multiplication table.
            Dim intNum As Integer
            Dim intNum1 As Integer = 0 'start at 0, not 1
            Dim intNum2 As Integer
    
            Integer.TryParse(txtNumber.Text, intNum)
    
            Do While intNum1 <= 9
                intNum2 = intNum * intNum1
                lblTable.Text = lblTable.Text &
                intNum.ToString & " * " &
                intNum1.ToString & " = " &
                intNum2.ToString & "    " & ControlChars.NewLine
                intNum1 += 1 'before continuing the loop, add 1 to intNum1
            Loop
    
     

     


    If you say it can`t be done then i`ll try it

    • Marked as answer by Godren Friday, February 22, 2019 12:00 AM
    Thursday, February 21, 2019 11:52 PM
  • Thanks!
    • Edited by Godren Friday, February 22, 2019 12:00 AM typo
    Thursday, February 21, 2019 11:59 PM