2015-09-06 63 views
0

我正在创建一个罗马数字转换器。我似乎无法让程序正常工作,因为我收到表达式期望的错误。我已经修复了其中的大部分,但最后两个都躲过了我。请帮忙解释一下。我正在使用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

有很多不必要的转换...字符串到int回到字符串和Case语句的另一个字符串转换。如果它是一个Int32变量,则不需要'CInt(intUserNumber)'。 – Plutonix

+0

为什么在你的'CInt(intUserNumber),'?'末尾有一个逗号?这是造成'表达预期'错误。但是你的代码有更多的问题。例如你的第二中频缺少了条件,然后。 –

回答

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。

相关问题