我正在玩学习VB.NET,通过制作一个基本的计算器并使代码慢慢变得更复杂,开始玩类和问题并遇到这个错误(见标题)并且无法理解什么它的意思是。这是我的完整代码:
Class Calculator
Private _num1, _num2, _result As Double
Private _operation, _output() As String
Private _ressuc As Boolean = True
Public Function input() As String()
Console.Write("Enter your first number: ")
_num1 = Console.ReadLine()
Console.Write("Operation? (Add, Subtract, Divide, Multiply, Modulus, Power): ")
_operation = Console.ReadLine()
Console.Write("Enter your second number: ")
_num2 = Console.ReadLine()
_output = calculate(_num1, _num2, _operation)
Return _output
End Function
Private Function calculate(num1 As Integer, num2 As Integer, operation As String) As String
Dim result As Double = Nothing
Dim ressuc As Boolean = True
Select Case operation
Case "add", "+"
result = num1 + num2
Case "subtract", "-"
result = num1 - num2
Case "divide", "/"
result = num1 / num2
Case "multiply", "*"
result = num1 * num2
Case "modulus", "%", "mod"
result = num1 Mod num2
Case "power", "^"
result = num1 ^ num2
Case Else
ressuc = False
End Select
If ressuc Then
calculate = ("Your answer is: " & result.ToString())
Else
calculate = ("Function '" & operation & "' not available.")
End If
End Function
End Class
Module Module1
Sub Main()
Dim calc As Calculator = New Calculator()
Console.WriteLine(calc.input())
Console.ReadLine()
End Sub
End Module
错误存在于此处:
_output = calculate(_num1, _num2, _operation)
计算函数(及其变量)带有下划线,并在标题中显示错误。有人可以向我解释为什么字符串不会从'计算'以及如何解决它?
答案 0 :(得分:0)
尝试从声明中的()
和output()
函数的String()
返回类型中删除input
Class Calculator
Private _num1, _num2, _result As Double
Private _operation, _output As String
Private _ressuc As Boolean = True
Public Function input() As String
Console.Write("Enter your first number: ")
这样做的原因是,当您声明以()
结尾的变量或使用As
关键字然后使用以()
结尾的类型时,您告诉它您想要一个那种类型的数组。因此,您可以从代码中删除()
,也可以将它们留在那里并访问所有项目,就像它们是数组一样。由于您没有访问或使用它们作为数组,因此更容易删除有问题的()
。
答案 1 :(得分:0)
您已将变量_output
定义为String Array :
Private _output() As String
与Private _output As String()
尝试从声明中删除括号,因为它看起来不应该使用字符串数组。
Private _output As String