如何判断输入是否为数字
初学AU3,尝试做一个最简单的计算器,做是做出来了,但是有问题,假如输入字符,就会得到错误结果,搞了半天,实在搞不定.恳请大侠指点.万分感谢.Global Const $GUI_ENVET_CLOSE = -3
Opt("GUIOnEventMode", 1)
Opt("MustDeclareVars", 1)
Local $Gui = GUICreate("简易计算器",450,100)
Local $y = "|+|-|*|/|"
Local $Input1 = GUICtrlCreateInput("",30,30,50,20)
Local $Combo = GUICtrlCreateCombo("",90,30,50,20,-1,-1)
Local $Input2 = GUICtrlCreateInput("",150,30,50,20)
Local $Button = GUICtrlCreateButton("=",210,30,50,20)
Local $Text = GUICtrlCreateEdit("",270,30,150,20)
GUICtrlSetData($Combo,$y)
GUISetOnEvent($GUI_ENVET_CLOSE,"main")
GUICtrlSetOnEvent($Input1,"main")
GUICtrlSetOnEvent($Input2,"main")
GUICtrlSetOnEvent($Button,"main")
GUISetState(@SW_SHOW)
While 1
GUISetBkColor(RandomColor())
Sleep(3000)
WEnd
Func main()
Local $n1 = GUICtrlRead($Input1)
Local $n2 = GUICtrlRead($Input2)
Local $x = GUICtrlRead($Combo)
Switch @GUI_CtrlId
Case $Button
Switch $x
Case "+"
GUICtrlSetData($Text,$n1 + $n2)
Case "-"
GUICtrlSetData($Text,$n1 - $n2)
Case "*"
GUICtrlSetData($Text,$n1 * $n2)
Case "/"
GUICtrlSetData($Text,$n1 / $n2)
EndSwitch
Case $GUI_ENVET_CLOSE
Exit
EndSwitch
EndFunc
Func RandomColor()
Local $temp = "0x"
Return $temp & Hex(Random(0,255,1),2) &Hex(Random(0,255,1),2) &Hex(Random(0,255,1),2)
EndFunc input 控件加 0x2000 样式
GUICtrlCreateInput("",30,30,50,20,0x2000) ;$ES_NUMBER = 0x2000 有可用的基本内置函数的, 建议有事没事都多翻翻帮助文档.
StringIsDigit 本帖最后由 user3000 于 2013-5-24 07:26 编辑
回复 2# afan
老大, 加那个样式就无法输入小数点了, 这是'计算器'呢.
发完上话,再多想了下, StringIsDigit 也不适用于检测带小数点的输入....看来只有自己定义个检测函数了 回复 4# user3000
没错,应该放过一个小数点的。简单的应用可以加 StringIsFloat 判断。
如果限制输入的话则需拦截分析,这方面以前就有过一些讨论和解决方法~ 回复 5# afan
这是个值得思考的问题,标记一下,有时间再来看,今晚上又有应酬~我勒个去~好几天没在家吃饭了~ MsgBox(0,0,Number('1.2')=1.2) 另外,官网上有个计算器的现成例子,可以参考下 #include <EditConstants.au3>
#include <WinAPI.au3> ; used for Lo/Hi word
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
Global Const $GUI_ENVET_CLOSE = -3
Opt("GUIOnEventMode", 1)
Opt("MustDeclareVars", 1)
Local $Gui = GUICreate("简易计算器", 450, 100)
Local $y = "|+|-|*|/|"
Local $Input1 = GUICtrlCreateInput("", 30, 30, 50, 20)
Local $Combo = GUICtrlCreateCombo("", 90, 30, 50, 20, -1, -1)
Local $Input2 = GUICtrlCreateInput("", 150, 30, 50, 20)
Local $Button = GUICtrlCreateButton("=", 210, 30, 50, 20)
Local $Text = GUICtrlCreateEdit("", 270, 30, 150, 20)
GUICtrlSetData($Combo, $y,"+")
GUISetOnEvent($GUI_ENVET_CLOSE, "main")
GUICtrlSetOnEvent($Input1, "main")
GUICtrlSetOnEvent($Input2, "main")
GUICtrlSetOnEvent($Button, "main")
GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
While 1
GUISetBkColor(RandomColor())
Sleep(3000)
WEnd
Func main()
Local $n1 = GUICtrlRead($Input1)
Local $n2 = GUICtrlRead($Input2)
Local $x = GUICtrlRead($Combo)
Switch @GUI_CtrlId
Case $Button
GUICtrlSetData($Text, Execute($n1&$x&$n2))
Case $GUI_ENVET_CLOSE
Exit
EndSwitch
EndFunc ;==>main
Func RandomColor()
Local $temp = "0x"
Return $temp & Hex(Random(0, 255, 1), 2) & Hex(Random(0, 255, 1), 2) & Hex(Random(0, 255, 1), 2)
EndFunc ;==>RandomColor
Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg
Local $hWndFrom, $iIDFrom, $iCode, $hWndEdit, $hWndEdit1, $a
If Not IsHWnd($Input1) Then $hWndEdit = GUICtrlGetHandle($Input1)
If Not IsHWnd($Input2) Then $hWndEdit1 = GUICtrlGetHandle($Input2)
$hWndFrom = $ilParam
$iIDFrom = _WinAPI_LoWord($iwParam)
$iCode = _WinAPI_HiWord($iwParam)
Switch $hWndFrom
Case $Input1, $Input2, $hWndEdit, $hWndEdit1
Switch $iCode
Case $EN_CHANGE ; Sent when the user has taken an action that may have altered text in an edit control
If $ilParam = $hWndEdit Then
$a = GUICtrlRead($Input1)
If StringRegExp($a, "[^\d\.]", 0) Then
Beep(500, 100)
GUICtrlSetData($Input1, StringRegExpReplace($a, "[^\d\.]",''))
EndIf
Else
$a = GUICtrlRead($Input2)
If StringRegExp($a, "[^\d\.]", 0) Then
Beep(500, 50)
GUICtrlSetData($Input2, StringRegExpReplace($a, "[^\d\.]",''))
EndIf
EndIf
Case $EN_KILLFOCUS ; Sent when an edit control loses the keyboard focus
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc其实用按钮式样的不是更方便嘛。(像系统自带的计算器) 嗯,果断收藏~~ 回复 9# pcbar
这个代码有问题哦,比如你输入12...35+12558...5.25这类的依然有效啊,但这些明显不是数字啊 本帖最后由 flyeblue 于 2013-5-30 07:31 编辑
这个最简单的解决办法,
一:直接调用系统的计算器,有现成的为啥不用?
二:你如果非用autoit编,直接用inputboxWhile True
Local $temp1 ,$yunsuan,$temp2,$iMsgBoxAnswer
$jieguo=0
$temp1=0
$temp2=0
$yunsuan=''
Do
$temp1=InputBox('注意','请输入第一个操作数:','','M')
If@error=1 Then ExitLoop 2
Until StringRegExp($temp1,"^\d+\.?\d*$")
Do
$yunsuan=InputBox('注意','请输入运算符(+-*/):','','M1')
If@error=1 Then ExitLoop 2
Until StringRegExp($yunsuan,'[\+\-\*/]')
Do
$temp2=InputBox('注意','请输入第二个操作数:','','M')
If $yunsuan='/' And $temp2<>0 Then ContinueLoop
If@error=1 Then ExitLoop 2
Until StringRegExp($temp2,"^\d+\.?\d*$")
Switch $yunsuan
Case '+'
$jieguo=$temp1+$temp2
Case '-'
$jieguo=$temp1-$temp2
Case '*'
$jieguo=$temp1*$temp2
Case '/'
$jieguo=$temp1/$temp2
EndSwitch
$iMsgBoxAnswer = MsgBox(65,"结果","结果为:" & @CRLF & $temp1&$yunsuan&$temp2&'='&$jieguo & @CRLF & "" & @CRLF & "点“确定”继续计算,点“取消”退出程序")
If $iMsgBoxAnswer = 2 Then;Cancel
ExitLoop
EndIf
WEnd 有点太长了 我不会,但是有个思路,用正则表达式判断行不? 这个问题是很伤脑筋啊
页:
[1]
2