locked
Roman Numerals RRS feed

  • Question

  • I am new at VB 2010 and am trying to covert 1-10 into roman numerals so that when you input a 1-10 the roman numeral I-X comes up, this is what I've got so far,and it won't display

    Public

     

     

    Class Form1

     

     

    Private Sub Btnconvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnconvert.Click

     

     

    'declare the intergers

     

     

    Dim intDecimal As Integer

     

     

    Dim intDecimal {}Roman_Numeral

     

     

     

    'displays the roman numerals

     

     

    If intDecimal = 1 Then

    Lblroman.Text =

     

    "I"

     

     

    ElseIf intDecimal = 2 Then

    Lblroman.Text =

     

    "II"

     

     

    ElseIf intDecimal = 3 Then

    Lblroman.Text =

     

    "III"

     

     

    ElseIf intDecimal = 4 Then

    Lblroman.Text =

     

    "IV"

     

     

    ElseIf intDecimal = 5 Then

    Lblroman.Text =

     

    "V"

     

     

    ElseIf intDecimal = 6 Then

    Lblroman.Text =

     

    "VI"

     

     

    ElseIf intDecimal = 7 Then

    Lblroman.Text =

     

    "VII"

     

     

    ElseIf intDecimal = 8 Then

    Lblroman.Text =

     

    "VIII"

     

     

    ElseIf intDecimal = 9 Then

    Lblroman.Text =

     

    "IX"

     

     

    ElseIf intDecimal = 10 Then

    Lblroman.Text =

     

    "X"

     

     

    End If

    Lblroman.Text ={

     

    ""} FormatNumber

     

     

    End

     

     

     

     

    End Sub

     

     

    Private Sub Btnexit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnexit.Click

     

     

    Me.Close()

    Please Help!! I know it must be something small that I missed?!

    Thanx

    Monday, November 1, 2010 8:00 PM

Answers

  • You haven't set intDecimal to a value.  For instance, if the decimal value is being entered into TextBox1 then you would use

    Dim intDecimal as Integer
    intDecimal = Val(TextBox1.Text)

    (but note that code will fail if the user has left the text box blank).

    You could also look at the use of an array to hold the numeral strings, so you could simply use intDecimal as an index into an array.

    lblRoman.Text=Numerals(intDecimal)

     

    • Marked as answer by Liliane Teng Wednesday, November 10, 2010 10:10 AM
    Monday, November 1, 2010 8:10 PM
  • Have a look at this:
     
    Dim iNumber as integer = 1
    Dim ary As String() = {"I""II""III", "IV""V""VI", "VII", "VIII", "IX", "X"}

    Lblroman.Text = ary(iNumber - 1) 'ary is zero based


    "CherryHillRich" wrote in message news:9635bb56-a9ff-4301-84a8-b1a3b2eca245...

    I am new at VB 2010 and am trying to covert 1-10 into roman numerals so that when you input a 1-10 the roman numeral I-X comes up, this is what I've got so far,and it won't display

    Public

     

     

    Class Form1

     

     

    Private Sub Btnconvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnconvert.Click

     

     

    'declare the intergers

     

     

    Dim intDecimal As Integer

     

     

    Dim intDecimal {}Roman_Numeral

     

     

     

    'displays the roman numerals

     

     

    If intDecimal = 1 Then

    Lblroman.Text =

     

    "I"

     

     

    ElseIf intDecimal = 2 Then

    Lblroman.Text =

     

    "II"

     

     

    ElseIf intDecimal = 3 Then

    Lblroman.Text =

     

    "III"

     

     

    ElseIf intDecimal = 4 Then

    Lblroman.Text =

     

    "IV"

     

     

    ElseIf intDecimal = 5 Then

    Lblroman.Text =

     

    "V"

     

     

    ElseIf intDecimal = 6 Then

    Lblroman.Text =

     

    "VI"

     

     

    ElseIf intDecimal = 7 Then

    Lblroman.Text =

     

    "VII"

     

     

    ElseIf intDecimal = 8 Then

    Lblroman.Text =

     

    "VIII"

     

     

    ElseIf intDecimal = 9 Then

    Lblroman.Text =

     

    "IX"

     

     

    ElseIf intDecimal = 10 Then

    Lblroman.Text =

     

    "X"

     

     

    End If

    Lblroman.Text ={

     

    ""} FormatNumber

     

     

    End

     

     

     

     

    End Sub

     

     

    Private Sub Btnexit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnexit.Click

     

     

    Me.Close()

    Please Help!! I know it must be something small that I missed?!

    Thanx


    Harry
    • Marked as answer by Liliane Teng Wednesday, November 10, 2010 10:10 AM
    Monday, November 1, 2010 8:24 PM
  •  

    Roman only used the number 1 to 3,899,999 in there numerical system.

    Here is a small class that can translate any integer ( 1 to 3,899,999 ) to roman numeral

    It is not a solution to your problem, but you might be interested to see

     

    Public Class Form1
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Val As Integer
        If Integer.TryParse(TextBox1.Text, Val) And Val > 0 And Val < 3899999 Then
          TextBox2.Text = Roman.Int2Roman(Val)
        Else
          TextBox2.Text = "Invalid value"
        End If
      End Sub
    End Class
    
    
    
    Class Roman
      Private Shared RomanToken() As String = {"(M)", "(C)(M)", "(D)", "(C)(D)", "(C)", _
                       "(X)(C)", "(L)", "(X)(L)", "(X)", "M(X)", _
                       "(V)", "M(V)", "M", "CM", "D", "CD", "C", _
                       "XC", "L", "XL", "X", "IX", "V", "IV", "I"}
      Private Shared TokenValue() As Integer = {1000000, 900000, 500000, 400000, 100000, 90000, _
                       50000, 40000, 10000, 9000, 5000, 4000, 1000, 900, _
                       500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}
    
      Public Shared Function Int2Roman(ByVal IntValue As Integer) As String
        If IntValue > 3899999 Or IntValue < 1 Then
          Throw New Exception("Overflow: Roman only used the numbers 1 to 3,899,999")
        Else
          Dim RomanNumeral As String = ""
          For index As Integer = 0 To RomanToken.Count - 1
            Dim Tp As Integer = IntValue \ TokenValue(index)
            IntValue = IntValue - Tp * TokenValue(index)
            While Tp > 0
              RomanNumeral &= RomanToken(index)
              Tp -= 1
            End While
          Next
          Return RomanNumeral
        End If
      End Function
    End Class
    
    • Proposed as answer by jinzai Tuesday, November 2, 2010 4:00 PM
    • Marked as answer by Liliane Teng Wednesday, November 10, 2010 10:12 AM
    Tuesday, November 2, 2010 2:48 PM

All replies

  • You haven't set intDecimal to a value.  For instance, if the decimal value is being entered into TextBox1 then you would use

    Dim intDecimal as Integer
    intDecimal = Val(TextBox1.Text)

    (but note that code will fail if the user has left the text box blank).

    You could also look at the use of an array to hold the numeral strings, so you could simply use intDecimal as an index into an array.

    lblRoman.Text=Numerals(intDecimal)

     

    • Marked as answer by Liliane Teng Wednesday, November 10, 2010 10:10 AM
    Monday, November 1, 2010 8:10 PM
  • Have a look at this:
     
    Dim iNumber as integer = 1
    Dim ary As String() = {"I""II""III", "IV""V""VI", "VII", "VIII", "IX", "X"}

    Lblroman.Text = ary(iNumber - 1) 'ary is zero based


    "CherryHillRich" wrote in message news:9635bb56-a9ff-4301-84a8-b1a3b2eca245...

    I am new at VB 2010 and am trying to covert 1-10 into roman numerals so that when you input a 1-10 the roman numeral I-X comes up, this is what I've got so far,and it won't display

    Public

     

     

    Class Form1

     

     

    Private Sub Btnconvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnconvert.Click

     

     

    'declare the intergers

     

     

    Dim intDecimal As Integer

     

     

    Dim intDecimal {}Roman_Numeral

     

     

     

    'displays the roman numerals

     

     

    If intDecimal = 1 Then

    Lblroman.Text =

     

    "I"

     

     

    ElseIf intDecimal = 2 Then

    Lblroman.Text =

     

    "II"

     

     

    ElseIf intDecimal = 3 Then

    Lblroman.Text =

     

    "III"

     

     

    ElseIf intDecimal = 4 Then

    Lblroman.Text =

     

    "IV"

     

     

    ElseIf intDecimal = 5 Then

    Lblroman.Text =

     

    "V"

     

     

    ElseIf intDecimal = 6 Then

    Lblroman.Text =

     

    "VI"

     

     

    ElseIf intDecimal = 7 Then

    Lblroman.Text =

     

    "VII"

     

     

    ElseIf intDecimal = 8 Then

    Lblroman.Text =

     

    "VIII"

     

     

    ElseIf intDecimal = 9 Then

    Lblroman.Text =

     

    "IX"

     

     

    ElseIf intDecimal = 10 Then

    Lblroman.Text =

     

    "X"

     

     

    End If

    Lblroman.Text ={

     

    ""} FormatNumber

     

     

    End

     

     

     

     

    End Sub

     

     

    Private Sub Btnexit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnexit.Click

     

     

    Me.Close()

    Please Help!! I know it must be something small that I missed?!

    Thanx


    Harry
    • Marked as answer by Liliane Teng Wednesday, November 10, 2010 10:10 AM
    Monday, November 1, 2010 8:24 PM
  •  

    Roman only used the number 1 to 3,899,999 in there numerical system.

    Here is a small class that can translate any integer ( 1 to 3,899,999 ) to roman numeral

    It is not a solution to your problem, but you might be interested to see

     

    Public Class Form1
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Val As Integer
        If Integer.TryParse(TextBox1.Text, Val) And Val > 0 And Val < 3899999 Then
          TextBox2.Text = Roman.Int2Roman(Val)
        Else
          TextBox2.Text = "Invalid value"
        End If
      End Sub
    End Class
    
    
    
    Class Roman
      Private Shared RomanToken() As String = {"(M)", "(C)(M)", "(D)", "(C)(D)", "(C)", _
                       "(X)(C)", "(L)", "(X)(L)", "(X)", "M(X)", _
                       "(V)", "M(V)", "M", "CM", "D", "CD", "C", _
                       "XC", "L", "XL", "X", "IX", "V", "IV", "I"}
      Private Shared TokenValue() As Integer = {1000000, 900000, 500000, 400000, 100000, 90000, _
                       50000, 40000, 10000, 9000, 5000, 4000, 1000, 900, _
                       500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}
    
      Public Shared Function Int2Roman(ByVal IntValue As Integer) As String
        If IntValue > 3899999 Or IntValue < 1 Then
          Throw New Exception("Overflow: Roman only used the numbers 1 to 3,899,999")
        Else
          Dim RomanNumeral As String = ""
          For index As Integer = 0 To RomanToken.Count - 1
            Dim Tp As Integer = IntValue \ TokenValue(index)
            IntValue = IntValue - Tp * TokenValue(index)
            While Tp > 0
              RomanNumeral &= RomanToken(index)
              Tp -= 1
            End While
          Next
          Return RomanNumeral
        End If
      End Function
    End Class
    
    • Proposed as answer by jinzai Tuesday, November 2, 2010 4:00 PM
    • Marked as answer by Liliane Teng Wednesday, November 10, 2010 10:12 AM
    Tuesday, November 2, 2010 2:48 PM