谁能帮忙用vb写一个阿拉伯数字转换成罗马数字的函数, 谢谢。比如传过来的数是1,给转换成I,传的数字是2,给转换成II
可以这样:
Public Function ToRomanNumeral(ByVal input As Integer) As String
If (input < 1 OrElse input > 999) Then
Throw New ArgumentOutOfRangeException("input", "Cannot convert integers outside of the range 1-999")
End If
Dim indices() As Integer = {1, 2, 3, 4, 5, 10, 50, 100, 500, 1000}
Dim romanNumerals() As String = {"I", "II", "III", "IV", "V", "X", "L", "C", "D", "M"}
Dim output As String = String.Empty
While input > 0
Dim i As Integer = 0
While indices(i) <= input
i += 1
End While
If i > 0 Then
i -= 1
End If
output = output & romanNumerals(i)
input = input - indices(i)
End While
Return output
End Function
如果对您有帮助,请采纳答案好吗,谢谢!
你把 数字定义一个 数组 var a = {1,2,3,4,。。。}
再把 罗马数字 定义一个 数组 var b ={“1”:“I”,“2”“II”,。。}
用两套循环 不就行了