简体   繁体   English

为什么我的输出不显示为小数?

[英]Why won't my output display as a decimal?

So I have this program that takes the values of items in a listbox and puts them into an array. 因此,我有一个程序,该程序将一个列表框中的项目值放入数组中。 The only problem I have is that when there is an even amount of numbers in the listbox, it should add up the two values and divide it by two to find the median. 我唯一的问题是,当列表框中有偶数个数字时,它应该将两个值相加并除以2以找到中位数。 For some reason, it refuses to output it as a decimal, just puts out an integer. 由于某种原因,它拒绝将其输出为小数,只输出一个整数。 I've tried to change all the values to decimals, but that only created a error, telling me that 'Option strict prevents conversions from long to decimal' or something like that (I need Op stric on). 我试图将所有值更改为十进制,但这只会产生一个错误,告诉我“ Option strict阻止了从长到十进制的转换”或类似的操作(我需要启用Op stric)。 Could anyone tell me why my output isn't coming out as a decimal?? 谁能告诉我为什么我的输出不是小数?

    Dim arrNumbers(lstNumbers.Items.Count), intLength, intNum1, intNum2 As Integer
    Dim decMedian As Decimal


    For i = 0 To lstNumbers.Items.Count - 1

        arrNumbers(i) = CInt(lstNumbers.Items(i))
    Next

    intLength = arrNumbers.Length - 1

    Array.Sort(arrNumbers)


    If intLength Mod 2 <> 0 Then
        MessageBox.Show("Median =" & arrNumbers(arrNumbers.GetUpperBound(0) \ 2 + 1).ToString)

    Else


        intNum1 = arrNumbers(arrNumbers.GetUpperBound(0) \ 2)

        intNum2 = arrNumbers((arrNumbers.GetUpperBound(0) \ 2) + 1)

        decMedian = (intNum1 + intNum2) \ 2

        MessageBox.Show("Median =" & decMedian.ToString("n2"))
    End If

Replace 更换

decMedian = (intNum1 + intNum2) \ 2

with

decMedian = (intNum1 + intNum2) / 2

\\ is the integer division operator. \\是整数除法运算符。 Check more on MSDN . MSDN上查看更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM