2015-09-06 61 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。

相關問題