-1

所以,我正在创建一个数学程序,它有几个函数,其中一个函数是小数位,这是什么:

用户被问到他希望他的答案针对不同的数学求解器显示多少位小数,例如,如果他说 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

结束子

4

2 回答 2

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

您可以使用标准数字格式字符串:定点(“F”)格式说明符,例如,“F2”表示两位小数,以格式化输出,例如:

Console.WriteLine("0.123456 to " & decimalPlaces & "D.P. is " & String.Format("{0:F" & decimalPlaces & "}", 0.123456))

使用单独的变量来读取用户输入意味着很容易,比如说,添加一个选项让他们输入“q”来退出。

我在需要时输入代码以使“place”复数:它使输出更整洁。


[顺便说一下,免费的Visual Studio 2017 社区版适用于 Windows 7 及更高版本。它具有诸如“字符串插值”之类的功能,可以使格式化输出更容易。]

于 2019-02-18T18:46:20.530 回答
1

您可以使用“:”字符来指定格式。您会注意到我的格式为“0”。&strDup()。如果 DP 为 3,这将输出 0.000 的格式,因为 StrDup 函数复制“0”字符 DP 次并将其附加到“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

我希望这有帮助。

于 2019-02-18T18:04:01.700 回答