"24點計算程式,輸入4個1~10之間的數字,組合計算值為24點的結果dim num(4)set re=New RegExpre.Pattern="\s+"re.Global=truedo while 1 str=re.replace(trim(inputbox("用空格分開,每個數在1~10之間:","輸入4個數"))," ") if str="" then exit do arr=split(str," ") if ubound(arr)=3 then for i=0 to 3 num(i)=arr(i)+0 next tim=now test=0 do while 1 "隨機排序 for i=3 to 1 step -1 j=int(rnd*i) if j<>i then t=num(j) num(j)=num(i) num(i)=t end if next "msgbox join(num," ") "隨機計算 sum=num(0) str=num(0) t2=2"上一次的運算子號 for i=1 to 3 "隨機計算方法:0+ 1- 2* 3/ if (sum<>0 and num(i)<>0) then if (sum mod num(i)=0 or num(i) mod sum=0) then t=4 else t=3 else t=3 end if t=int(rnd*t) "如果上次為+-、本次為*/則給前面的加上括號 if t2<2 and t>1 then str="(" & str & ")" t2=t select case t case 0:sum=sum+num(i):str=str & " + " & num(i) case 1: if sum>num(i) then sum=sum-num(i) str=str & " - " & num(i) else sum=num(i)-sum if i>1 then str=num(i) & " - (" & str &")" else str=num(i) & " - " & str end if case 2:sum=sum*num(i):str=str & " * " & num(i) case 3: if sum>num(i) then sum=sum/num(i) str=str & " / " & num(i) else sum=num(i)/sum if i>1 then str=num(i) & " / (" & str &")" else str=num(i) & " / " & str end if end select next "判斷結果 if sum=24 then msgbox str,,"成功": exit do test=test+1 if (now-tim)*24*3600>5 then msgbox "測試"& test &"次:" & join(num," "),,"失敗":exit do loop end ifloop
我用VBS做過,建議你用VBS吧:把下面的程式碼複製貼上到記事本,儲存為一個.VBS的檔案,例如24.VBS,然後滑鼠雙擊就可以執行它。
全部程式碼如下:
"24點計算程式,輸入4個1~10之間的數字,組合計算值為24點的結果dim num(4)set re=New RegExpre.Pattern="\s+"re.Global=truedo while 1 str=re.replace(trim(inputbox("用空格分開,每個數在1~10之間:","輸入4個數"))," ") if str="" then exit do arr=split(str," ") if ubound(arr)=3 then for i=0 to 3 num(i)=arr(i)+0 next tim=now test=0 do while 1 "隨機排序 for i=3 to 1 step -1 j=int(rnd*i) if j<>i then t=num(j) num(j)=num(i) num(i)=t end if next "msgbox join(num," ") "隨機計算 sum=num(0) str=num(0) t2=2"上一次的運算子號 for i=1 to 3 "隨機計算方法:0+ 1- 2* 3/ if (sum<>0 and num(i)<>0) then if (sum mod num(i)=0 or num(i) mod sum=0) then t=4 else t=3 else t=3 end if t=int(rnd*t) "如果上次為+-、本次為*/則給前面的加上括號 if t2<2 and t>1 then str="(" & str & ")" t2=t select case t case 0:sum=sum+num(i):str=str & " + " & num(i) case 1: if sum>num(i) then sum=sum-num(i) str=str & " - " & num(i) else sum=num(i)-sum if i>1 then str=num(i) & " - (" & str &")" else str=num(i) & " - " & str end if case 2:sum=sum*num(i):str=str & " * " & num(i) case 3: if sum>num(i) then sum=sum/num(i) str=str & " / " & num(i) else sum=num(i)/sum if i>1 then str=num(i) & " / (" & str &")" else str=num(i) & " / " & str end if end select next "判斷結果 if sum=24 then msgbox str,,"成功": exit do test=test+1 if (now-tim)*24*3600>5 then msgbox "測試"& test &"次:" & join(num," "),,"失敗":exit do loop end ifloop