将以下代码存为 html 文件,预先将所有游戏者编好号码,然后在 html 页面中选好杀手人数和游戏总人数,点击“开始”按钮,程序会随机选择作为杀手的游戏者的编号。
<html>
<center>
<table border=5 bordercolor=blue borderlight=green>
<tr><td align=center><font size=4 color=red face="Arial, Helvetica, sans-serif">
<strong> * 随机产生杀手程序 * </strong></font></td></tr>
<tr><td align=center height=80>
<SCRIPT LANGUAGE="JavaScript">
<!--
function numbers() {
var nummenu = document.lotto.numbercount;
var numbercount = nummenu.options[nummenu.selectedIndex].value*1;
var maxnumbers = document.lotto.maxnum.value*1;
if (numbercount > maxnumbers) {
alert("Be sure to select a max lottery number value!");
}
else {
var ok = 1;
r = new Array (numbercount);
for (var i = 1; i <= numbercount; i++) {
r[i] = Math.round(Math.random() * (maxnumbers-1))+1;
}
for (var i = numbercount; i >= 1; i--) {
for (var j = numbercount; j >= 1; j--) {
if ((i != j) && (r[i] == r[j])) ok = 0;
}
}
if (ok) {
var output = "";
for (var k = 1; k <= numbercount; k++) {
output += "杀手" + k + " = " + r[k] + "\n";
}
document.lotto.results.value = output;
}
else numbers();
}
}
// -->
</script>
<form name="lotto">
<table width=100% border=0>
<td align=center>杀手数量
<select name="numbercount">
<option value="1">1
<option value="2">2
<option value="3"selected>3
<option value="4">4
<option value="5">5
<option value="6">6
<option value="7">7
<option value="8">8
<option value="9">9
<option value="10">10
</select>
<br>总人数 <input type=text name=maxnum value="15" size=2 maxlength=2><br>
<input type=button value=" 开 始 " onClick="numbers()">
<p>
<textarea name="results" rows=5 cols=15></textarea>
</table>
</form></td></tr></table>
</html>