Vb.net 使用方法和功能提示用户输入数据和输出计算结果
我被这个VisualBasic问题的一个特定部分卡住了。给你Vb.net 使用方法和功能提示用户输入数据和输出计算结果,vb.net,visual-studio,function,methods,vb6,Vb.net,Visual Studio,Function,Methods,Vb6,我被这个VisualBasic问题的一个特定部分卡住了。给你 Write a program that is used to perform calculations on two numbers. • The program should make use of the following methods: 1. Main() – Calls method getdata() 2. Getdata() – asks the user for data and type of calcul
Write a program that is used to perform calculations on two numbers.
• The program should make use of the following methods:
1. Main() – Calls method getdata()
2. Getdata() – asks the user for data and type of calculation to perform then calls
the respective calculation method i.e.
– Add()
– Subtract()
– Multiply()
– Divide()
3. Displaydata() – called by the main method to display the results obtained
这就是我到目前为止所做的
Module Module1
Sub Main()
Dim a, b, c As Integer
Console.WriteLine("Please enter first number: ")
a = Console.ReadLine()
Console.WriteLine("Please enter second number: ")
b = Console.ReadLine()
Console.WriteLine("Which type of calculation would you like to perform? ")
Console.WriteLine("1. Addition ")
Console.WriteLine("2. Multiplication ")
Console.WriteLine("3. Addition ")
Console.WriteLine("4. Subtraction ")
c = Console.ReadLine()
If c = 1 Then
add(a, b)
Else
If c = 2 Then
subtract(a, b)
Else
If c = 3 Then
multiply(a, b)
Else
divide(a, b)
End If
End If
End If
End Sub
Sub add(ByVal y As Integer, ByVal z As Integer)
Console.WriteLine("The sum of the two numbers is " & y + z)
End Sub
Sub subtract(ByVal y As Integer, ByVal z As Integer)
Console.WriteLine("The difference between the two numbers is " & y - z)
End Sub
Sub multiply(ByVal y As Integer, ByVal z As Integer)
Console.WriteLine("The product of the two numbers is " & y * z)
End Sub
Sub divide(ByVal y As Integer, ByVal z As Integer)
Console.WriteLine("The division of the two numbers is " & y / z)
End Sub
End Module
如您所见,我的主要问题是引入getData()和displayData()函数来提示用户输入和显示结果。我尝试过很多次,但每次我都会出现至少25个错误,程序无法运行。我真的很感激任何形式的帮助。先谢谢你
正如所问的,下面是我插入getData()和displayData()方法的失败尝试
首先,我有一些在编写vb.net时应该考虑的注释 不要在一行中声明变量。
Dim a As Integer
Dim b As Integer
Dim c As Integer
为变量指定有意义的名称。
Dim FirstNumber As Integer
Dim SecondNumber As Integer
Dim Operation As Integer
使用选择案例而不是多个ElseIf
Select Case Operation
Case 1
add(FirstNumber, SecondNumber)
Case 2
multiply(FirstNumber, SecondNumber)
Case 3
subtract(FirstNumber, SecondNumber)
Case 4
divide(FirstNumber, SecondNumber)
End Select
使用函数而不是子函数返回值。
您可以使用返回结果的函数,而不是使用所有这些子函数,例如:
Function add(ByVal y As Integer, ByVal z As Integer) As Integer
Return y + z
End Function
至于你的问题。问题是,执行sub后,程序将被关闭,因为它已完成。为了防止出现这种情况,并允许查看结果,请在主子控制台的末尾写入。ReadLine(),这将防止程序关闭
此外,您在操作选择中有一个错误-您写了两次加法…为什么不显示插入getdata和displaydata失败的尝试?。通过这种方式,我们可以帮助您理解并解决这些问题。您需要获得老师的许可,才能在这样的网站上复制您的家庭作业。根据cc by sa 3.0许可证的条款,任何人都可以自由复制此处的任何内容。很怀疑你的老师是否同意根据该许可证重新分配他的作品,特别是因为你没有遵守许可证规则。一个指向原始内容的链接和一个识别作者的链接是最低要求。伙计,这是一个混乱的代码…你在那里做了什么。O@Steve,我已将插入所述文件的失败尝试包括在内methods@Hans帕桑,谢谢你的批评……但我不是你公开宣称的学生,因此,我无法获得许可。我正在单独学习VB,以便在我的个人项目中使用。感谢您提供非常有价值的信息。正如你所说,我一输入第一个数字,程序就关闭了,我想知道为什么。我会尽快采纳你们的建议。似乎你们中的大多数人即使在受到了很多直言不讳的批评之后也会把头埋在沙子里。嗯……如果你有一个特别的问题,只需编辑你的问题,我们会尽最大努力。
Function add(ByVal y As Integer, ByVal z As Integer) As Integer
Return y + z
End Function