規則
随機數字為答案,在限定次數内猜中,猜錯會提示猜大或者猜小,并且繼續猜測。
優化
範圍為變量存儲,猜小了則用修改左邊界,猜大了修改右邊界
漏洞
不在指定範圍内猜測,則會擴大範圍。
代碼
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