在我的VB学校项目中,我们正在做区域计算器。这是我目前的计划。当我在“输入侧宽度”(第45行)处设置断点时,它表示输入为0,但根据我标记为“GetVal”的函数,它应该是readline()。其余的功能部件没有使用GetVal,因为我想让它首先在正方形上工作。
Imports System.Console
Module Module1
Private Function SquareArea(ByVal Length As Single, ByVal Width As Single) As Single
Return Length * Width
End Function
Private Function CircleArea(ByVal Radius As Single) As Single
Return 3.14159 * (Radius * Radius)
End Function
Private Function TriArea(ByVal Base As Single, ByVal Height As Single) As Single
Return (Base * Height) / 2
End Function
Private Function GetVal(ByVal Input As Single) As Single
Input = ReadLine()
Return Input
End Function
Sub Main()
Dim Length, Width, answer, Radius, Base, Height, Input As Single
Dim Validinput As Integer
Dim Choice As String
Do
WriteLine("Press the nunmpad for the area you want to find")
WriteLine("1 for Rectangle/Square, 2 for Circle, 3 for Triangle")
Do
Validinput = True
Choice = ReadKey(True).Key
If Choice <> ConsoleKey.NumPad1 And Choice <> ConsoleKey.NumPad2 And Choice <> ConsoleKey.NumPad3 Then Validinput = False
Loop Until Validinput
'Square/Rectangle'
If Choice = ConsoleKey.NumPad1 Then
WriteLine("Enter Side Length")
Do
Validinput = True
Try
GetVal(Input = Length)
Input = Length
Catch ex As Exception
WriteLine("Error, Enter a number")
Validinput = False
End Try
Loop Until Validinput = True
WriteLine("Enter Side Width")
Do
Validinput = True
Try
GetVal(Input = Width)
Input = Width
Catch ex As Exception
WriteLine("Please enter a number")
Validinput = False
End Try
Loop Until Validinput = True
WriteLine(SquareArea(Length, Width))
End If
'Circle'
If Choice = ConsoleKey.NumPad2 Then
WriteLine("Enter Radius")
Do
Validinput = True
Try
Radius = ReadLine()
WriteLine(CircleArea(Radius))
Catch ex As Exception
WriteLine("Please Enter a number")
Validinput = False
End Try
Loop Until Validinput = True
End If
'Triangle'
If Choice = ConsoleKey.NumPad3 Then
WriteLine("Enter Base Length")
Do
Validinput = True
Try
Base = ReadLine()
Catch ex As Exception
WriteLine("Please Enter a number")
Validinput = False
End Try
Loop Until Validinput
WriteLine("Enter Height Size")
Do
Validinput = True
Try
Height = ReadLine()
Catch ex As Exception
WriteLine("Please Enter a number")
Validinput = False
End Try
Loop Until Validinput
WriteLine(TriArea(Base, Height))
End If
WriteLine("")
Loop
End Sub
End Module
------------编辑------------------
如果有人遇到同样的问题,我的最终工作代码。我知道它不漂亮,但确实如此:
Imports System.Console
Module Module1
Private Function SquareArea(ByVal Length As Single, ByVal Width As Single) As Single
Return Length * Width
End Function
Private Function CircleArea(ByVal Radius As Single) As Single
Return Math.PI * (Radius * Radius)
End Function
Private Function TriArea(ByVal Base As Single, ByVal Height As Single) As Single
Return (Base * Height) / 2
End Function
Private Function GetVal(ByVal Input As Single) As Short
Dim ValueOK As Boolean
Do
ValueOK = True
Try
Input = Readline()
Catch ex As Exception
ValueOK = False
WriteLine("Error, please enter a number")
End Try
Loop Until ValueOK
Return Input
End Function
Sub Main()
Dim Length, Width, Radius, PI, Base, Height, Input As Short
Dim Validinput As Integer
Dim Choice As String
Do
WriteLine("Press the nunmpad for the area you want to find")
WriteLine("1 for Rectangle/Square, 2 for Circle, 3 for Triangle")
Do
Validinput = True
Choice = ReadKey(True).Key
If Choice <> ConsoleKey.NumPad1 And Choice <> ConsoleKey.NumPad2 And Choice <> ConsoleKey.NumPad3 Then Validinput = False
Loop Until Validinput
'Square/Rectangle'
If Choice = ConsoleKey.NumPad1 Then
WriteLine("Enter Side Length")
Validinput = True
Length = GetVal(123456.0)
Input = Length
WriteLine("Enter Side Width")
Validinput = True
Width = GetVal(123456.0)
Input = Width
WriteLine(SquareArea(Length, Width))
End If
'Circle'
If Choice = ConsoleKey.NumPad2 Then
WriteLine("Enter Radius")
Validinput = True
Radius = GetVal(123456.0)
Input = Radius
WriteLine(CircleArea(Radius))
End If
'Triangle'
If Choice = ConsoleKey.NumPad3 Then
WriteLine("Enter Base Length")
Validinput = True
Base = GetVal(123456.0)
Input = Base
WriteLine("Enter Height Size")
Validinput = True
Height = GetVal(123456.0)
Input = Height
WriteLine(TriArea(Base, Height))
End If
WriteLine("")
Loop
End Sub
End Module
答案 0 :(得分:1)
首先,如果你有Option Strict On
,这甚至都不会编译。你的部分问题是:
GetVal(Input = Length)
在这里,您基本上是在调用GetVal(True)
或GetVal(False)
,因为Input = Length
是一个比较,并且评估为真或假。
此外,您正在调用一个返回值的Function
,但对返回值不执行任何操作。这里有2个选项:
将您的功能更改为ByRef
而不是ByVal
,这样您就可以覆盖传入的变量。
将GetVal(Input = Length)
等更改为Length = GetVal(123456.0)
。事实上,你编写函数的方式,你传递给GetVal
的数字甚至都不重要,因为它会立即被覆盖在函数内部。你可以简单地改变GetVal
根本不接受任何数字,这没有任何区别。
解决方案#2是更好的解决方案,也是正确的方法。
你需要这样的东西:
Option Strict On
Imports System.Console
Module Module1
Private Function SquareArea(ByVal Length As Single, ByVal Width As Single) As Single
Return (Length * Width)
End Function
Private Function CircleArea(ByVal Radius As Single) As Single
Return (Math.PI * (Radius * Radius))
End Function
Private Function TriArea(ByVal Base As Single, ByVal Height As Single) As Single
Return (Base * Height) / 2.0
End Function
Private Function GetVal() As Single
Dim userInput As String = ReadLine()
Return Convert.ToSingle(userInput)
End Try
End Function
Sub Main()
Dim Length As Single
Dim Width As Single
Dim answer As Single
Dim Radius As Single
Dim Base As Single
Dim Height As Single
Dim Validinput As Integer
Dim Choice As String
Do
WriteLine("Press the nunmpad for the area you want to find")
WriteLine("1 for Rectangle/Square, 2 for Circle, 3 for Triangle")
Do
Validinput = True
Choice = ReadKey(True).Key
If Choice <> ConsoleKey.NumPad1 And Choice <> ConsoleKey.NumPad2 And Choice <> ConsoleKey.NumPad3 Then Validinput = False
Loop Until Validinput
'Square/Rectangle'
If Choice = ConsoleKey.NumPad1 Then
WriteLine("Enter Side Length")
Do
Validinput = True
Try
Length = GetVal()
Catch ex As Exception
WriteLine("Error, Enter a number")
Validinput = False
End Try
Loop Until Validinput
WriteLine("Enter Side Width")
Do
Validinput = True
Try
Width = GetVal()
Catch ex As Exception
WriteLine("Please enter a number")
Validinput = False
End Try
Loop Until Validinput
WriteLine(SquareArea(Length, Width))
End If
'Circle'
If Choice = ConsoleKey.NumPad2 Then
WriteLine("Enter Radius")
Do
Validinput = True
Try
Radius = GetVal()
WriteLine(CircleArea(Radius))
Catch ex As Exception
WriteLine("Please Enter a number")
Validinput = False
End Try
Loop Until Validinput
End If
'Triangle'
If Choice = ConsoleKey.NumPad3 Then
WriteLine("Enter Base Length")
Do
Validinput = True
Try
Base = GetVal()
Catch ex As Exception
WriteLine("Please Enter a number")
Validinput = False
End Try
Loop Until Validinput
WriteLine("Enter Height Size")
Do
Validinput = True
Try
Height = GetVal()
Catch ex As Exception
WriteLine("Please Enter a number")
Validinput = False
End Try
Loop Until Validinput
WriteLine(TriArea(Base, Height))
End If
WriteLine("")
Loop
End Sub
End Module
此外,这是使用Select Case
语句而不是多个If
语句的情况。