2017-04-25 2 views
0

Ich bin neu in Visual Basics Codierung und fragte mich, ob jemand wusste, warum die End-Gesamtkosten nicht funktioniert. Zur Zeit halte ich C als Endwert bekommenVisual Basic Currency Error hält mich den Wert von C

Hier wird die Variable Bit, so dass Sie die Datentypen, sehen kann ich

Dim Height As Decimal 
Dim HeightValidation As Boolean 
Dim Width As Decimal 
Dim WidthValidation As Boolean 
Dim TotalArea As Decimal 
Dim Paint As String 
Dim PaintValidation As Boolean 
Dim Cost As Decimal 
Dim FinalCost As Decimal 
Dim UndercoatValidation As Boolean 
Dim Undercoat As String 

eingestellt haben und hier ist das letzte Bit des Codes

Do Until UndercoatValidation = True 
    UnderCoat = InputBox("Would you like to add an Undercoat to your purchase?") 
    If Undercoat = "Yes" Then 
     FinalCost = Cost + (TotalArea * 0.5) 
     UndercoatValidation = True 
    ElseIf Undercoat = "No" Then 
     FinalCost = Cost 
    Else 
     UndercoatValidation = False 
    End If 
Loop 
MsgBox("Thank you for using the Paint Calculator") 
MsgBox("Your total cost is"(FormatCurrency(FinalCost))) 

Vielen Dank im Voraus. Übrigens, bevor ich das Währungsbit hinzugefügt habe, hat es funktioniert.
So ändern sich die Werte der Kosten in Abhängigkeit davon, ob der Benutzer eine Unterwolle haben möchte oder nicht, welche Art von Farbe sie verwenden und die Fläche des Raumes.

Do Until HeightValidation = True 
    Height = InputBox("Enter the height of the room you are workin in: ") 'Gains users value 
    If (2 <= Height) AndAlso (Height <= 6) Then 'Only allows number between 2 and 6 
     HeightValidation = True 'breaks the loop if the requirements are met 
    Else 
     HeightValidation = False 'continues the loop if the requirements are not met 
    End If 
Loop 

Do Until WidthValidation = True 
    Width = InputBox("Enter the width of the room you are workin in: ") 'Gains users value 
    If (1 <= Width) AndAlso (Width <= 25) Then 'Only allows number between 1 and 25 
     WidthValidation = True 'breaks the loop if the requirements are met 
    Else 
     WidthValidation = False 'continues the loop if the requirements are not met 
    End If 
Loop 

TotalArea = Height * Width 

Do Until PaintValidation = True 
    MsgBox("There are 3 different types of paint you could you, which are: Luxary which costs £1.75 per square metre; standard which costs £1 per square metre and economy wich costs £0.45 per square metre") 
    Paint = InputBox("Which type of paint will you be using to paint the walls?") 
    If Paint = "standard" Then 
     Cost = TotalArea * 1 
     PaintValidation = True 
    ElseIf Paint = "economy" Then 
     Cost = TotalArea * 0.45 
     PaintValidation = True 
    ElseIf Paint = "luxary" Then 
     Cost = TotalArea * 1.75 
     PaintValidation = True 
    Else 
     PaintValidation = False 
    End If 
Loop 

Das ist der Rest des Codes, der die Kosten ausrechnet, sorry für den ganzen Code, der nicht kommentiert wird.

+0

Was sind die Werte von 'Cost' und' TotalArea'? Außerdem würde ich empfehlen, keine Do/Loop mit einer InputBox zu verwenden. Der Benutzer muss weder Ja noch Nein eingeben. Wenn er in Kleinbuchstaben geschrieben wird, schlägt die Logik fehl. Verwenden Sie einfach eine MessageBox mit den Schaltflächen Ja und Nein, und führen Sie Ihre Logik basierend auf dem Rückgabewert aus. –

+0

Die Werte von Kosten und Gesamtfläche ändern sich je nachdem, was der Benutzer eingibt. Bei Bedarf kann ich dieses Bit des Codes hochladen. Und mit der logischen Sache werde ich meinen Code verbessern, um so zu sein, wie du gesagt hast. Entschuldigung, noch neu bei vb – IcarusFlight

Antwort

1
MsgBox("Your total cost is"(FormatCurrency(FinalCost))) 

Das bedeutet „zeigen die character at positionFormatCurrency(FinalCost) aus dem String "Your total cost is".

Offenbar das Ergebnis FormatCurrency(FinalCost) ist implizit konvertierbar 11, so dass es die "c" zeigt.

Sie Anscheinend gemeint

MsgBox("Your total cost is " & FormatCurrency(FinalCost)) 

Bitte verwenden Sie Option Strict On, um diese Art von Problemen in der Zukunft nicht zu haben.