天天看点

【UiBot】练习一简单猜数字游戏

规则

随机数字为答案,在限定次数内猜中,猜错会提示猜大或者猜小,并且继续猜测。

优化

范围为变量存储,猜小了则用修改左边界,猜大了修改右边界

漏洞

不在指定范围内猜测,则会扩大范围。

代码

Dim intInput = 0,intRand //intRand是用户输入的值
Dim intLeftBorder = 1 , intRightBorder = 99 , intcount = 6 //极小值和极大值和轮数

intRand = CInt(Rnd()*100)//intRand是随机0-1,转化为整型

intInput = Dialog.InputBox("请输入"& intLeftBorder &"到"& intRightBorder&"的数字","猜数字","",True)//弹窗
intInput = CInt(intRand)//将用户输入的值转化为int


Do While intRand <> intInput//只要不对就开始循环
	
	If intcount = 0 //次数用完啦
		Dialog.MsgBox("次数用完啦","游戏结束","0","1",0)
		Break
	End If
	
	If intInput>intRand //大于目标值,设置极小值为输入值
		intRightBorder = intInput
		intInput = Dialog.InputBox("请输入"& intLeftBorder &"到"& intRightBorder&"的数字","大了哦,还有"&intcount&"次机会","",True)//弹窗
		intInput = CInt(intInput) //将用户输入的值转化为整型
	Else //小于目标值,设置极大值为输入值
		intLeftBorder = intInput
		intInput = Dialog.InputBox("请输入"& intLeftBorder &"到"& intRightBorder&"的数字","小了哦,还有"&intcount&"次机会","",True)//弹窗
		intInput = CInt(intInput) //将用户输入的值转化为整型
	End If
	
	intcount = intcount - 1
	
Loop

If intRand = intInput
	Dialog.MsgBox("回答正确,答案是"&intRand,"游戏结束","0","1",0) //回答正确,跳出循环,提示后结束
End If



           

继续阅读