2015-10-28 35 views
0

我正在爲我的Visual Basic類創建一個項目,即創建數字檢查。該任務要求我們輸入一個支票金額,將其轉化爲單詞。例如,$ 1,200.00需要輸出「一千二百元」使用案例/方法將檢查金額轉換爲單詞

大多數情況下,我的代碼工作。我正在使用switch語句。最初的任務是讓我們的支票達到9,999的價值,但隨着我們繼續建立,我們需要能夠轉換爲99,999。

正如我所說的,我一直在使用一系列case語句,但意識到這是一個非常「硬代碼」的方式,並希望創建一個可以檢查這些類型的東西的方法,但是我仍然對Visual Basic不熟悉,並且真的不知道從哪裏開始或者在這種情況下適用什麼(我們沒有真正的示例)。

這是我的WriteCheck方法,大部分分配/轉換。

'Convert check value from a text field to a double' 
    Try 
     checkValue = checkInput.Text 
    Catch ex As InvalidCastException 
     MessageBox.Show("You must enter a numbers to write a check.") 
    End Try 

    'Operation to convert number to String' 

    thousands = checkValue \ 1000 
    hundreds = checkValue Mod 1000 
    hundreds = hundreds \ 100 
    tens = checkValue Mod 100 
    tens = tens \ 10 
    ones = checkValue Mod 10 
    ones = ones \ 1 

    'Case for thousands' 
    Select Case thousands & hundreds & tens 
     Case 1 
      tempStringT = "One" 
     Case 2 
      tempStringT = "Two" 
     Case 3 
      tempStringT = "Three" 
     Case 4 
      tempStringT = "Four" 
     Case 5 
      tempStringT = "Five" 
     Case 6 
      tempStringT = "Six" 
     Case 7 
      tempStringT = "Seven" 
     Case 8 
      tempStringT = "Eight" 
     Case 9 
      tempStringT = "Nine" 

    End Select 

    'Case for hundreds' 
    Select Case hundreds 
     Case 1 
      tempStringH = "one" 
     Case 2 
      tempStringH = "two" 
     Case 3 
      tempStringH = "three" 
     Case 4 
      tempStringH = "four" 
     Case 5 
      tempStringH = "five" 
     Case 6 
      tempStringH = "six" 
     Case 7 
      tempStringH = "seven" 
     Case 8 
      tempStringH = "eight" 
     Case 9 
      tempStringH = "nine" 

    End Select 

    'Case for tens' 
    Select Case tens Or ones 
     Case 1 
      tempStringTens = "one" 
     Case 2 
      tempStringTens = "twenty" 
     Case 3 
      tempStringTens = "thirty" 
     Case 4 
      tempStringTens = "fourty" 
     Case 5 
      tempStringTens = "fifty" 
     Case 6 
      tempStringTens = "sixty" 
     Case 7 
      tempStringTens = "seventy" 
     Case 8 
      tempStringTens = "eighty" 
     Case 9 
      tempStringTens = "ninety" 

    End Select 

    If tempStringTens <> "one" Then 
     'Case for ones' 
     Select Case ones 
      Case 1 
       tempStringO = "one" 
      Case 2 
       tempStringO = "two" 
      Case 3 
       tempStringO = "three" 
      Case 4 
       tempStringO = "four" 
      Case 5 
       tempStringO = "five" 
      Case 6 
       tempStringO = "six" 
      Case 7 
       tempStringO = "seven" 
      Case 8 
       tempStringO = "eight" 
      Case 9 
       tempStringO = "nine" 

     End Select 

     lblConverted.Text = tempStringT & " thousand " & tempStringH & " hundred " & tempStringTens & " " & tempStringO & " dollars " & change & "/100" 

    End If 



    If tempStringTens = "one" Then 
     Select Case ones 
      Case 1 
       tempStringO = "eleven" 
      Case 2 
       tempStringO = "twelve" 
      Case 3 
       tempStringO = "thirteen" 
      Case 4 
       tempStringO = "fourteen" 
      Case 5 
       tempStringO = "fifteen" 
      Case 6 
       tempStringO = "sixteen" 
      Case 7 
       tempStringO = "seventeen" 
      Case 8 
       tempStringO = "eighteen" 
      Case 9 
       tempStringO = "nineteen" 
     End Select 

     lblConverted.Text = tempStringT & " thousand " & tempStringH & " hundred " & tempStringO & " dollars" 

    End If 

End Sub 
+1

這是「VB」還是「VBA」?很明顯,這個問題在一定程度上是通用的,但是您可能希望用最佳專業知識標記爲「VB」。 – Smandoli

+0

謝謝,我實際上很難弄清楚如何標記這個,所以我只給了一個鏡頭! –

+0

這個問題是不正確的,因爲您要求我們構建整個算法。你應該自己嘗試一下,然後在問這裏之前達到一個可以接受的階段。作爲一個快速提示,爲了減少這種大量的硬編碼,您必須找到常見的部分,只有易於自動化的位變化。例如,代替「Dim val1 As String =」1000「如果param = 2那麼val1 =」2000「ElseIf param = 3然後Val1 =」3000「,等等,你可以這樣做:'function calculateVal1(param As Integer )As String 返回param.ToString()&「000」 End Function'和'val1 = calculateVal1(param)'。 – varocarbas

回答

1

這是我的問題解決辦法。如果需要,可以通過添加或刪除BigNumbers中的項目並將num的範圍擴大到Long以上來輕鬆擴大或縮小解決方案。 (書面,它將爲人數達到999,999,999,999,999工作)。

Public Function NumberToText(ByVal num As Long) As String 
    Dim BigNumbers() As String = {"", " Thousand", " Million", " Billion", " Trillion"} 
    Dim TextParts() As String = {} 
    If num < 0 Then 
     Return "Checks cannot be written for negative amounts." 
    ElseIf num >= 10^((BigNumbers.Length) * 3) Then 
     Return "This number exceeds the current maximum value of " & NumberToText(10^((BigNumbers.Length) * 3) - 1) & "." 
    End If 
    Dim LoopCount As Integer = 0 
    While num >= 1000 
     ReDim Preserve TextParts(TextParts.Length) 
     If num Mod 1000 > 0 Then 
      TextParts(TextParts.GetUpperBound(0)) = ThreeDigitText(num Mod 1000) & BigNumbers(LoopCount) 
     End If 
     num = num \ 1000 
     LoopCount += 1 
    End While 
    ReDim Preserve TextParts(TextParts.Length) 
    TextParts(TextParts.GetUpperBound(0)) = ThreeDigitText(num) & BigNumbers(LoopCount) 
    If Array.IndexOf(TextParts, "Error") > -1 Then 
     Return "An unknown error occurred while converting this number to text." 
    Else 
     Array.Reverse(TextParts) 
     Return Join(TextParts) 
    End If 
End Function 

Private Function ThreeDigitText(ByVal num As Integer) As String 
    If num > 999 Or num < 0 Then 
     Return "Error" 
    Else 
     Dim h As Integer = num \ 100 'Hundreds place 
     Dim tempText As String = "" 
     If h > 0 Then 
      tempText = OneDigitText(h) & " Hundred" 
     End If 
     num -= h * 100 
     If num > 0 And Not tempText = "" Then 
      tempText &= " " 
     End If 
     If num > 9 And num < 20 Then 
      Dim DoubleDigits() As String = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"} 
      Return tempText & DoubleDigits(num - 10) 
     Else 
      Dim TensPlace() As String = {"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"} 
      Dim t As Integer = num \ 10 'Tens place 
      num -= t * 10 
      If t > 1 Then 
       tempText &= TensPlace(t - 2) 
       If num > 0 Then 
        tempText &= " " 
       End If 
      End If 
      If num > 0 Then 
       tempText &= OneDigitText(num) 
      End If 
      Return tempText 
     End If 
    End If 
End Function 

Private Function OneDigitText(ByVal num As Integer) As String 
    If num > 9 Or Num < 0 Then 
     Return "Error" 
    Else 
     Dim SingleDigits() As String = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"} 
     Return SingleDigits(num) 
    End If 
End Function 

由於這是學校,你可能會想我的代碼部分適應自己,而不是複製整個事情。 (教師通常可以告訴您何時從互聯網上獲取代碼,特別是如果您無法解釋每一行。)如果您對此有任何疑問,請將它們發送到我的個人資料中列出的電子郵件。

+0

謝謝你這麼大的一個例子。我會咀嚼這個,如果我有任何問題,請務必發送電子郵件! –