[已解決]如何計算正值 負值的平均值阿?
本帖最后由 tcpuuu 于 2012-8-13 16:14 编辑$gu_Form1 = GUICreate("EXCEL",600,100,-1,-1)
$BUT1 = GUICtrlCreateButton("計算+值and-值平均值",2,50,126,26,0x01)
local $EO=6,$KU=2,$KI=56,$KO=17
local $SL
For $i = 0 To 9
$SL[$i] = GUICtrlCreateLabel("",$EO,$KU,$KI,$KO,0x01)
$EO+=57
GUICtrlSetBkColor($SL[$i],0xFFFFFF)
GUICtrlSetData($SL[$i],-3+$i)
GUICtrlSetFont($SL[$i],12)
Next
$X=0
$Y=0
;=============================================================================
GUISetState(@SW_SHOW)
Local $nMsg
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case -3
Exit
Case $BUT1
For $i = 0 To 9
IF GUICtrlRead($SL[$i]) > 0 THEN $X+=1;找出正的有幾個
IF GUICtrlRead($SL[$i]) < 0 THEN $Y+=1
Next
msgbox(0,"","正的平均值")
msgbox(0,"","負的平均值")
EndSwitch
WEnd GUICtrlRead 读出来的是字符串
你得把它转成 数值
$gu_Form1 = GUICreate("EXCEL", 600, 100, -1, -1)
$BUT1 = GUICtrlCreateButton("計算+值and-值平均值", 2, 50, 126, 26, 0x01)
Local $EO = 6, $KU = 2, $KI = 56, $KO = 17
Local $SL
For $i = 0 To 9
$SL[$i] = GUICtrlCreateLabel("", $EO, $KU, $KI, $KO, 0x01)
$EO += 57
GUICtrlSetBkColor($SL[$i], 0xFFFFFF)
GUICtrlSetData($SL[$i], -3 + $i)
GUICtrlSetFont($SL[$i], 12)
Next
$X = 0
$Y = 0
;=============================================================================
GUISetState(@SW_SHOW)
Local $nMsg
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case -3
Exit
Case $BUT1
$sum1 = 0
$n1 = 0
$avg1 = 0
$sum2 = 0
$n2 = 0
$avg2 = 0
$data = 0
For $i = 0 To 9
$data = Number(GUICtrlRead($SL[$i]))
If $data > 0 Then
$sum1 += $data
$n1 += 1
EndIf
If $data < 0 Then
$sum2 += $data
$n2 += 1
EndIf
Next
If $n1 Then
$avg1 = $sum1 / $n1
MsgBox(0, "正数平均值=", $avg1)
Else
MsgBox(0, "抱歉", "正数为0个")
EndIf
If $n2 Then
$avg2 = $sum2 / $n2
MsgBox(0, "负数平均值=", $avg2)
Else
MsgBox(0, "抱歉", "负数为0个")
EndIf
EndSwitch
WEnd
本帖最后由 tcpuuu 于 2012-8-12 19:47 编辑
嗯嗯! 謝謝veket_linux 解答
經整理後 如下
Case $BUT1
Local $sum1=0,$n1=0,$avg1=0,$sum2=0,$n2=0,$avg2=0
For $i = 0 To 9
$dva = Number(GUICtrlRead($SL[$i]))
If $dva > 0 Then
$sum1 += $dva
$n1 += 1
EndIf
If $dva < 0 Then
$sum2 += $dva
$n2 += 1
EndIf
Next
If $n1 Then $avg1 = Round($sum1 / $n1,2)
If $n2 Then $avg2 = Round($sum2 / $n2,2)
MsgBox(0, "正平均值=", $avg1)
MsgBox(0, "負平均值=", $avg2) 回复 4# tcpuuu
请楼主结贴,谢谢诶!
页:
[1]