我正在创建一个罗马数字转换器。我似乎无法使程序正常运行,因为我得到表达式预期错误。我已经解决了大部分问题,但最后两个问题让我不知所措。请帮忙来解决这个问题。我正在使用Visual Basic 2013.这是我到目前为止的代码。
'Get the input from the user and test to see it is an integer.
If Integer.TryParse (txtUserInput.Text, CInt(intUserNumber), Then
'Display the Roman Numeral.
Select Case (CStr(intUserNumber()))
Case CStr(1)
lblRomanNumeral.Text = "I"
Case CStr(2)
lblRomanNumeral.Text = "II"
Case CStr(3)
lblRomanNumeral.Text = "III"
Case CStr(4)
lblRomanNumeral.Text = "IV"
Case CStr(5)
lblRomanNumeral.Text = "V"
Case CStr(6)
lblRomanNumeral.Text = "VI"
Case CStr(7)
lblRomanNumeral.Text = "VII"
Case CStr(8)
lblRomanNumeral.Text = "VIII"
Case CStr(9)
lblRomanNumeral.Text = "IX"
Case CStr(10)
lblRomanNumeral.Text = "X"
End Select
If
lblRomanNumeral.Text = "Not an integer"
Else
End If
End
End Sub
答案 0 :(得分:1)
Expression Expected
错误是由于第一个IF语句末尾的额外逗号造成的。
If Integer.TryParse (txtUserInput.Text, CInt(intUserNumber), <-- this comma
您的代码中也存在其他错误。例如你的第二个IF语句缺少条件和THEN关键字等。你还有很多从String到Integer的不必要的转换,反之亦然。
但是回到你的程序,你根本不需要那么长的SELECT CASE
语句系列。这可以使用Choose
函数在一行中完成,如下所示:
'Get the input from the user and test to see it is an integer.
If Integer.TryParse(txtUserInput.Text, intUserNumber) Then
'Display the Roman Numeral.
Select Case intUserNumber
Case 1 To 10
lblRomanNumeral.Text = Choose(intUserNumber, "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X").ToString
Case Else
lblRomanNumeral.Text = "integer value out of range!"
End Select
Else
lblRomanNumeral.Text = "Not an integer"
End If
HTH。