Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
139 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.]


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...