How can I fix this code and add this decimal place function to my output of another function

六月ゝ 毕业季﹏ 提交于 2019-12-12 06:57:26

问题


So, I am creating a mathematical program, it has a couple of functions, one of the function is a decimal place holder, what this is:

the user is asked how many decimal places he would like his answers to be shown for the different mathematical solvers, so example is if he says 3 then my answer for another function e.g 1+1 would equal 2.000

They are asked a range between 1 and 5, I have the code for this but do not know how to implement it for the answer of the functions

'Decimal place

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

'Quadratic Equation

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


回答1:


Nowadays it is usual to avoid GoTo statements as they can make following and maintaining the code difficult.

You can instead use various looping structures, for example:

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

You can use a standard numeric format string: The Fixed-Point ("F") Format Specifier which would be, say, "F2" for two decimal places, to format the output, for example:

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

Using a separate variable to read the user input into means that it would be easy to, say, add an option for them to enter "q" to quit.

I put in code to make "place" plural when needed: it makes the output neater.


[Incidentally, the free Visual Studio 2017 Community Edition is available for Windows 7 and later. It has things like "string interpolation" which can make formatting output easier.]




回答2:


You can use the ":" character to specify the format. You will notice that I have the format as "0." & strDup(). This will output a format of 0.000 if DP is 3 as the StrDup function duplicates the "0" character DP times and appends it to "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

I hope this helps.



来源:https://stackoverflow.com/questions/54752809/how-can-i-fix-this-code-and-add-this-decimal-place-function-to-my-output-of-anot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!