vb.net Visual Basic,预期语句结束 - 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20086907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Visual Basic, End of Statement Expected - Function
提问by James Owen
I've been having issues trying to fix an "End of Statement Expected" Error for this program I've been working on. Everything else seems to work fine until I've started working with the Function Statement that I called Payments. In this function I'm trying to calculate the monthlyBal for every month.
我一直在尝试修复我一直在处理的这个程序的“预期语句结束”错误。在我开始使用我称为 Payments 的 Function Statement 之前,其他一切似乎都运行良好。在这个函数中,我试图计算每个月的monthlyBal。
The exact position that I'm getting this error is;
我收到此错误的确切位置是;
While monthlyBal > 0
monthlyBal = Payments(tempBalances, monthlyRate)
I've added the rest of the code below.
我已经添加了下面的其余代码。
Module CreditCardCalculator
Sub DisplayCreditCards(ByVal cardNames() As String, ByVal cardAPRs() As Double, ByVal cardBalances() As Double)
Const _SPACE As String = " "
Dim count As Integer = 1
System.Console.WriteLine("The Order of Credit Cards To Pay Off")
System.Console.WriteLine("------------------------------------")
For pos = 0 To cardNames.Length - 1
System.Console.WriteLine("Credit Card " & count & ": ")
System.Console.WriteLine(_SPACE & "NAME: " & cardNames(pos))
System.Console.WriteLine(_SPACE & "APRs: " & cardAPRs(pos) &"%")
System.Console.WriteLine(_SPACE & "BALANCE: " & cardBalances(pos))
System.Console.WriteLine()
count = count + 1
Next
End Sub
Sub OrderofCreditCards(ByRef cardNames() As String, ByRef cardAPRs() As Double, ByRef cardBalances() As Double, ByVal SIZE as Integer)
Dim firstInput As String
Dim secondInput As String
Dim swapNames(SIZE) As String
Dim swapAPRs(SIZE) As Double
Dim swapBalances(SIZE) As Double
System.Console.WriteLine("Which Credit Card would you like to payoff first?")
firstInput = Console.ReadLine()
For pos = 0 To cardNames.Length - 1
If firstInput = cardNames(pos) Then
swapNames(0) = cardNames(pos)
swapAPRs(0) = cardAPRs(pos)
swapBalances(0) = cardBalances(pos)
Exit For
End If
Next
System.Console.WriteLine("Which Credit Card would you like to payoff second?")
secondInput = Console.ReadLine()
For pos = 0 To cardNames.Length - 1
If secondInput = cardNames(pos) Then
swapNames(1) = cardNames(pos)
swapAPRs(1) = cardAPRs(pos)
swapBalances(1) = cardBalances(pos)
Exit For
End If
Next
For pos = 0 To cardNames.Length - 1
If cardNames(pos) <> swapNames(0) Then
If cardNames(pos) <> swapNames(1) Then
swapNames(2) = cardNames(pos)
swapAPRs(2) = cardAPRs(pos)
swapBalances(2) = cardBalances(pos)
Exit For
End If
End If
Next
cardNames = swapNames
cardAPRs = swapAPRs
cardBalances = swapBalances
End Sub
Sub DisplayMenu()
System.Console.WriteLine("CREDIT CARD CALCULATOR MENU")
System.Console.WriteLine("===========================")
System.Console.WriteLine("OPTION 1. Display Total Number Of Payments Required To Pay Off Each
Card. ")
System.Console.WriteLine()
System.Console.WriteLine("OPTION 2. Display The Number Of Years, Or Months To Pay Off Each Card.
")
System.Console.WriteLine()
System.Console.WriteLine("OPTION 3. Display The Balance To Payoff Each Card and Total Amount To
Payoff All Cards Combined. ")
System.Console.WriteLine()
System.Console.WriteLine("OPTION 4. Exit The Program. ")
System.Console.WriteLine()
System.Console.WriteLine
("=============================================================================")
System.Console.WriteLine("Instructions: Type The Number That Is Next To The Option You Want To
Execute. ")
End Sub
Function Payments(ByVal tempBalances As Double, ByVal monthlyRate As Double) As Double
Const ISSUECHARGE As Integer = 3
Dim avgMonthlyBal As Double
Dim interest As Double
Dim minimumPayment As Double
avgMonthlyBal = tempBalances
interest = monthlyRate
avgMonthlyBal = avgMonthlyBal + interest
minimumPayment = avgMonthlyBal * ISSUECHARGE
avgMonthlyBal = avgMonthlyBal - minimumPayment
Return avgMonthlyBal
End Function
Sub Main()
Const MAX_SIZE AS Integer = 2
Const BILLPERIOD As Integer = 30
Const MONTHSINYEAR As Integer = 12
Dim creditCards(MAX_SIZE) As String
creditCards(0) = "Discover"
creditCards(1) = "Visa"
creditCards(2) = "Master Card"
Dim creditCardAPRs(MAX_SIZE) As Double
creditCardAPRs(0) = 12.99
creditCardAPRs(1) = 7.5
creditCardAPRs(2) = 18.9
Dim creditCardBalances(MAX_SIZE) As Double
creditCardBalances(0) = 300
creditCardBalances(1) = 400
creditCardBalances(2) = 500
Dim myInput As String
Dim optionNum As String
Dim tempBalances As Double
Dim monthlyRate As Double
Dim numberofDays As Integer
Dim monthlyBal As Double
DisplayCreditCards(creditCards, creditCardAPRs, creditCardBalances)
System.Console.WriteLine("Would you like to adjust the order of the Credit Card?")
System.Console.WriteLine()
System.Console.WriteLine("If Yes, type 'Y' --------------------- If No, type 'N'")
myInput = Console.ReadLine()
If myInput = "Y" Then
OrderofCreditCards(creditCards, creditCardAPRs, creditCardBalances, MAX_SIZE)
End If
System.Console.WriteLine()
Console.Clear()
DisplayCreditCards(creditCards, creditCardAPRs, creditCardBalances)
System.Console.WriteLine()
DisplayMenu()
optionNum = Console.ReadLine()
numberOfDays = 30
Select Case optionNum
Case "1"
For pos = 0 To creditCards.Length - 1
tempBalances = creditCardBalances(pos) * numberOfDays / BILLPERIOD
monthlyRate = creditCardAPRs(pos) / MONTHSINYEAR
monthlyBal = creditCardBalances(pos)
While monthlyBal > 0
monthlyBal = Payments(tempBalances, monthlyRate)
System.Console.WriteLine(monthlyBal)
End While
Next
Case "2"
System.Console.WriteLine("Case 2")
Case "3"
System.Console.WriteLine("Case 3")
Case "4"
System.Console.WriteLine("Exiting The Program... ")
Console.Read()
Case Else
System.Console.WriteLine("Error: Not a valid option from the menu. ")
System.Console.WriteLine("Exiting The Program... ")
End Select
End Sub
End Module
It's either something small and I haven't spotted it yet or, I'm not working with functions correctly since the compiler seems to point to it. Like, I said everything else seems to be working fine and compiled correctly, until I added the "Function Payments Statement" and the stuff inside the "Case 1 Statment".
它要么很小,我还没有发现它,要么我没有正确使用函数,因为编译器似乎指向它。就像,我说其他一切似乎都运行良好并且编译正确,直到我添加了“功能付款声明”和“案例 1声明”中的内容。
回答by paxdiablo
One thing that seems a little suspicious is that you have a number of (apparently) spaces at the end of the whileline. I'd start by getting rid of those and trying again.
似乎有点可疑的一件事是您在行尾有许多(显然)空格while。我会先摆脱这些,然后再试一次。
It maybe that there's some funny characters in there that just got pasted into Stack Overflow as spaces, and these may be causing grief to the compiler.
这可能是那里面有一些奇怪的字符是刚刚粘贴到堆栈溢出的空间,而这些可能是导致悲痛编译器。
It's a long shot since you have a few other lines like that but it's the only strangeness I can see at or around that line.
这是一个很长的镜头,因为你还有其他几条这样的线,但这是我在这条线上或附近看到的唯一奇怪之处。
回答by Idle_Mind
Once I fixed DisplayMenu() it all compiled just fine:
一旦我修复了 DisplayMenu() ,它就会编译得很好:
Sub DisplayMenu()
System.Console.WriteLine("CREDIT CARD CALCULATOR MENU")
System.Console.WriteLine("===========================")
System.Console.WriteLine("OPTION 1. Display Total Number Of Payments Required To Pay Off Each Card. ")
System.Console.WriteLine()
System.Console.WriteLine("OPTION 2. Display The Number Of Years, Or Months To Pay Off Each Card. ")
System.Console.WriteLine()
System.Console.WriteLine("OPTION 3. Display The Balance To Payoff Each Card and Total Amount To Payoff All Cards Combined. ")
System.Console.WriteLine()
System.Console.WriteLine("OPTION 4. Exit The Program. ")
System.Console.WriteLine()
System.Console.WriteLine("=============================================================================")
System.Console.WriteLine("Instructions: Type The Number That Is Next To The Option You Want To Execute. ")
End Sub
回答by Ahmad
In VB, you can not split strings in multiple lines like this
在 VB 中,您不能像这样将字符串分成多行
'This will give an error
System.Console.WriteLine("OPTION 3. Display The Balance To Amount To
Payoff Each Card and Total Payoff All Cards Combined. ")
Even if you remove the blank line in the middle, this will also give you an error
即使去掉中间的空行,这也会报错
'This will also give an error
System.Console.WriteLine("OPTION 3. Display The Balance To Amount To
Payoff Each Card and Total Payoff All Cards Combined. ")
Here is what you can do if you must break long lines of code in VB:
如果必须在 VB 中中断长代码行,您可以执行以下操作:
'This is acceptable
System.Console.WriteLine("OPTION 3. Display The Balance To Amount To " & _
"Payoff Each Card and Total Payoff All Cards Combined. ")
Or alternatively, (if you must have blank lines in between your code) you can type it like this:
或者,(如果您的代码之间必须有空行)您可以这样输入:
'This is also acceptable
System.Console.WriteLine("OPTION 3. Display The Balance To Amount To " & _
"" & _
"Payoff Each Card and Total Payoff All Cards Combined. ")
回答by soniya mahadik
con.ConnectionString = "Data Source=ADMIN-PC\SQLEXPRESS;Initial Catalog=" BILLING SYSTEM";Integrated Security=True"
con.ConnectionString = "Data Source=ADMIN-PC\SQLEXPRESS;Initial Catalog="BILLING SYSTEM";Integrated Security=True"
This Expression will have error BC30205 End of statement expected.
此表达式将出现错误 BC30205 End of statement expected。

