2016-03-31 46 views
1

我正在研究一個項目,它是某種二次方程和其他部分的計算器。其中一項任務是製作一個精確度選項,以使用戶設置小數點後1至5位之間的舍入。Visual basic製作全局回合函數

我已經構建了某種功能已經做到這一點,但是我需要的是當程序有兩個不同的結果(即二次方程)時如果我使用我的函數來加入兩個答案和四捨五入。

我該如何讓保存結果的變量以及將結果舍入的函數能夠在不加入它們的情況下存儲兩個不同的答案?或者我寫的代碼不是它應該是怎麼樣的?有小費嗎?

我正在使用Math.round函數。

這是代碼;

Function Accuracy() 
    Dim choice As Integer = 0 
    Console.WriteLine("This is the accuracy option") 
    Console.WriteLine("Enter the decimals places to be set from 1 - 5") 
    DP = Console.ReadLine() 
    Console.WriteLine("The current decimals places are set to: " & DP) 
    ... 

Function round() 
    Result = Math.Round(Result, DP) 
End Function 

我試圖使它的工作方式是通過使結果=該計算的「結果」,然後運行round()函數

  • DP和結果是全局變量。
+0

是你使用VB.net或VBA?標籤不清晰...我會創建一個類,可以存儲值,一個真實的結果和一個圓整的... –

+1

基於'控制檯'和'數學'類我會說這不是VBA –

+0

永遠不會故意拋出計算準確度遠。只有爲了人類的利益才能做到這一點,可以使用複合格式。 –

回答

-1

好吧,這裏基本上是我會建立的結構。請記住,我不知道你的項目,甚至約二次方程少...

文件MyResult.vb

Public Class MyResult 
    Public Property Result as Double 'I suppose 
    Public Property RoundedDecimals as Integer 'We hold here the rouding 
    Public Readonly Property RoundedResult as Double 
    Get 
    Return Math.Round(Result, RoundedDecimals) 
    End Get 
    End Property 

    Public Sub New(Result as Double) 
    Me.Result = Result 
    RoundedDecimals = 5 
    End Sub 

    '... End of your class 
End Class 

文件MainModule.vb

Public Module MainModule 

    Sub Main() 
    'Calculate your stuff 
    ...   

    'Create your result 
    Dim Result As New MyResult(CalculatedValue) 

    'Ask for Rounding 
    Dim round = 0 
    Do 
     Console.WriteLine("Enter number of decimals : ") 
     round = Console.ReadLine() 
    while not Integer.TryParse(round, Result.RoundedDecimals) 

    'Displaying rounded result 
    Console.WriteLine("Rounded Result is {0}", Result.RoundedResult) 

    '... 
    End Sub() 

End Module 
+0

併爲了使用戶輸入舍入值只是做RoundedDecimals = console.readline()? – Bart123

+0

並且會在module1中找到類還是創建新類?如果是這樣,我怎麼鏈接類與模塊,我知道如何在c + +但idk如何在VB – Bart123

+0

我如何使計算的結果,然後使用類來圓? – Bart123