因此,我正在创建一个数学程序,它具有几个函数,其中一个函数是小数位占位符,这是什么:
询问用户1到5之间的小数位数。他希望针对不同的数学函数显示答案。
假设用户选择答案为小数点后4位,然后他进行设置,然后选择二次方程求解器,要求他提供A,B,C值,一旦计算出该值,就说他输入1,-8和15,答案是-3和5。但是,由于他选择的答案是小数点后4位,答案应为-3.0000和5.0000
我无法使用它,这是下面的代码,有人可以帮我解决这个问题吗,现在被困了几天了
我尝试将数字四舍五入到用户输入的小数位:不起作用
“小数位规则”
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
End Sub
答案 0 :(得分:0)
重要的是WriteLine
使用的精度,而不是四舍五入的精度。因此,请使用float format specifiers:
Console.WriteLine("First Root, (Root1) = {0:F"&DP&"}", x1)
因为Fxx
是一个浮点数,其小数点后有xx
个数字,所以不需要四舍五入。