【发布时间】:2014-02-01 23:39:35
【问题描述】:
即使如此,我也是一个非常初学者的程序员。我必须在高中时将编程课程作为我的课程之一,所以我正在尽最大努力通过对它的了解不多。话虽如此,请对我放轻松。
对于一个项目,我需要回到我为之前的作业创建的两个程序,一个用于确定等差数列的程序,以及一个用于计算立方体或球体体积的程序。
第一条指令是编写程序,要求用户定义所有变量。例如,程序会要求用户输入立方体和球体程序的一侧的长度或半径值。
我相信我在编写程序时已经这样做了,但是我正在努力解决第二组指示,其中指出“包括关键字 Try、Catch 和 Final,并让它们正常运行(即,如果用户定义了一个不可能的计算,必须显示错误消息;如果没有,即使只是显示答案,也必须有一些动作表明没有错误)"。
我真的不知道如何将这些语句放入我的代码中。如果有人可以在我的代码中解释或给我一个例子,我将非常感激!这是我的代码:
立方体或球体的体积
Sub Main()
Dim Type As String
Dim Size As String
Dim Length_Radius As Double
Dim Output As Double
Dim Value As Double
Console.WriteLine("Would you like to calculate the volume of a (C)ube or a (S)phere? *press either C or S then enter to continue*")
Type = Console.ReadLine
Type = Convert.ToString(Type)
Value = Asc(Type)
If (Value = 67) Or (Value = 99) Then
Console.WriteLine("The equation is x^3 where x is the length of a side. What would you like the value of x to be?")
Size = Console.ReadLine
Length_Radius = Convert.ToDouble(Size)
Output = (Length_Radius * Length_Radius * Length_Radius)
Console.WriteLine("The value of the volume of the Cube is: " & Output & ".")
ElseIf (Value = 83) Or (Value = 115) Then
Console.WriteLine("The equation is 4/3*pi*r^3 where r is the radius. What would you like the value of the radius to be?")
Size = Console.ReadLine
Length_Radius = Convert.ToDouble(Size)
Output = ((4 / 3) * 3.14 * (Length_Radius * Length_Radius * Length_Radius))
Console.WriteLine("The value of the volume of a sphere is: " & Output & ".")
ElseIf (Value <> 83) Or (Value <> 115) Or (Value <> 67) Or (Value <> 99) Then
Console.WriteLine("You have inputted an incorrect value.")
End If
Console.ReadLine()
End Sub
算术序列
Sub Main()
Dim Letters As String
Dim a_n, d, a_one As Double
Dim Output As Double
Console.WriteLine("This program will perform the arithmetic sequence (a(n) = a(1) + d(a(n) - 1)")
Console.WriteLine("What would you like the value of 'a(n)' to be? *The total number of times the sequence will repeat.*")
Letters = Console.ReadLine
a_n = Convert.ToDouble(Letters)
Console.WriteLine("What would you like the value of 'a(1)' to be? *The starting value.*")
Letters = Console.ReadLine
a_one = Convert.ToDouble(Letters)
Console.WriteLine("What would you like the value of 'd' to be? *The number that the equation is multiplied by*")
Letters = Console.ReadLine
d = Convert.ToDouble(Letters)
Output = (a_one + d * (a_n - 1))
Console.WriteLine("The value of the arithmetic sequence is: " & Output & ".")
Console.ReadLine()
End Sub
【问题讨论】:
-
您是否阅读过有关 try, catch, finally 的文档?
-
如果通过文档您的意思是实际上学习了 Try、Catch 和 finally 是什么,那么是的,我确实在给定的课程中学到了一些关于它们的知识,但我似乎无法理解编程。
-
您是否尝试过文档中的一些示例?
标签: vb.net visual-studio exception try-catch finally