因此,我正在创建一个数学程序,它具有几个函数,其中一个函数是小数位占位符,这是什么:
询问用户,他希望为不同的数学解算器显示多少小数位数,例如,如果他说3,则我对另一个函数(例如1 + 1)的答案等于2.000
询问他们1到5之间的范围,我有相应的代码,但是不知道如何实现此功能
'小数位
Sub Accuracy()
Line1:
Dim DP Console.WriteLine("Please Enter the Decimial Limit between 1-5: ") DP = Double.Parse(Console.ReadLine()) If (DP > 5) Then Console.WriteLine("Error, Decimial Limit is between 1 and 5, Please Try Again!") GoTo Line1
Else
DP = DP
Console.Write("Decimial Limit has been Set Succuesfully to " & DP & " Decimal Places")
End If
End Sub
'二次方程式
Sub QuadraticFunction() Dim a, b, c As Integer Dim d, x1, x2 As Double
line1:
Console.WriteLine("Please Input a Non-Zero Number, A: ")
a = Console.ReadLine()
If (a = 0) Then
Console.WriteLine("Error, Number must not be 0, Try Again!")
GoTo line1
End If
Console.WriteLine("Please Input The Value of, B: ")
b = Console.ReadLine()
Console.Write("Please Input the Value of, C: ")
c = Console.ReadLine()
d = b * b - (4 * a * c)
If (d = 0) Then
Console.WriteLine("Both Roots Are Equal.")
x1 = -b / (2.0 * a)
x2 = x1
x1 = Math.Round(x1, DP)
x2 = Math.Round(x1, DP)
Console.WriteLine("First Root, (Root1) = {0}", x1)
Console.WriteLine("Second Root, (Root2) = {0}", x2)
ElseIf (d > 0) Then
Console.WriteLine("Both Roots are Real and Different")
x1 = (-b + Math.Sqrt(d)) / (2 * a)
x2 = (-b - Math.Sqrt(d)) / (2 * a)
x1 = (Math.Round(x1, DP))
x2 = (Math.Round(x2, DP))
Console.WriteLine("First Root, (Root1) = {0}", x1)
Console.WriteLine("Second Root, (Root2) = {0}", x2)
Else
Console.Write("Root are Imaginary " & "No Solution")
End If
结束子
答案 0 :(得分:2)
如今,通常避免使用GoTo语句,因为它们会使跟踪和维护代码变得困难。
您可以改用各种循环结构,例如:
Option Infer On
Option Strict On
Module Module1
Dim decimalPlaces As Integer = 2 ' default value
Sub Accuracy()
Dim validEntry = False
Do
Console.Write("Please enter the decimal limit between 1-5: ")
Dim a = Console.ReadLine()
If Integer.TryParse(a, decimalPlaces) Then
validEntry = decimalPlaces >= 1 AndAlso decimalPlaces <= 5
End If
If Not validEntry Then
Console.WriteLine("Error: value must be an integer from 1 to 5.")
End If
Loop Until validEntry
Dim plural = ""
If decimalPlaces <> 1 Then plural = "s"
Console.Write("Decimal limit has been set succesfully to " & decimalPlaces & " decimal place" & plural)
End Sub
Sub QuadraticFunction()
' other code...
Console.WriteLine(String.Format("First Root, (Root1) = {0:F" & decimalPlaces & "}", x1))
' other code...
End Sub
Sub Main()
' code ....
End Sub
End Module
您可以使用标准的数字格式字符串:The Fixed-Point ("F") Format Specifier(例如,对于小数点后两位为“ F2”)来格式化输出,例如:
Console.WriteLine("0.123456 to " & decimalPlaces & "D.P. is " & String.Format("{0:F" & decimalPlaces & "}", 0.123456))
使用单独的变量将用户输入读入意味着,很容易添加一个选项,让他们输入“ q”退出。
我在需要时放入代码以使“放置”复数:它使输出更整洁。
[偶然地,免费的Visual Studio 2017 Community Edition可用于Windows 7及更高版本。它具有“字符串插值”之类的功能,可以简化格式输出。]
答案 1 :(得分:1)
您可以使用“:”字符指定格式。 您会注意到我的格式为“ 0”。 &strDup()。如果DP为3,则将输出0.000格式,因为StrDup函数将DP字符重复“ 0”并将其附加到“ 0”。
Module Module1
Dim DP As Integer '<---- Notice that it is not declared in a sub().
Sub Main()
Dim DecimalPlace As Integer
Dim blnGoodAnswer As Boolean
Dim x1 As Double = 3
Dim x2 As Double = 16
blnGoodAnswer = False
Do Until blnGoodAnswer
Console.WriteLine("Please Enter the Decimial Limit between 1-5: ")
Integer.TryParse(Console.ReadLine(), DecimalPlace)
If (DecimalPlace < 1) Or (DecimalPlace > 5) Then
Console.WriteLine("Error, Decimial Limit is between 1 and 5, Please Try Again!")
Else
DP = DecimalPlace
blnGoodAnswer = True
Console.WriteLine("Decimial Limit has been Set Succuesfully to " &
DP & " Decimal Places")
Console.WriteLine("First Root, (Root1) = " & String.Format("{0:0." &
StrDup(DP, "0") & "}", x1))
Console.WriteLine("Second Root, (Root2) = " & String.Format("{0:0." &
StrDup(DP, "0") & "}", x2))
End If
Loop
End Sub
End Module
我希望这会有所帮助。