87,993
社区成员
发帖
与我相关
我的任务
分享var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if (computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
console.log("Computer: " + computerChoice);
function compare(choice1, choice2) {
if (choice1 === choice2) {
return "The result is a tie!";
} else if (choice1 === "rock") {
if (choice2 === "scissors") {
return "rock wins";
} else {
return "paper wins";
}
} else if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
} else {
return "scissors wins";
}
} else {
if (choice2 === "rock") {
return "rock wins";
} else if (choice2 === "paper") {
return "scissors wins";
}
}
}
compare(userChoice, computerChoice);
var items = ['rock','paper','scissors']
var userChoice = prompt("Do you choose rock(0), paper(1) or scissors(2)?");
var computerChoice = Math.random()*3;
console.log("Computer: " + items[computerChoice]);
function compare(choice1, choice2){
// 无论用户输入什么内容,都将其转换成数字以对应 items 数组,然后比较
}
compare(userChoice, computerChoice);
<input type="button" value="用户出剪刀" onclick="cq(0);" />
<input type="button" value="用户出石头" onclick="cq(1);" />
<input type="button" value="用户出布" onclick="cq(2);" />
<script type="text/javascript">
function cq(user) {
var arr = ['剪刀','石头','布'];
var ai = Math.random()*3>>0;
var str = user==ai?"平手":(user+1)%3==ai?"电脑赢了":"用户赢了";
alert("用户出"+arr[user]+", 电脑出"+arr[ai]+", "+str);
}
var arr = ['剪刀','石头','布']
var userChoice = arr.indexOf(prompt("请输入剪刀,石头或布")) * 3;
if(userChoice === -3){console.log("输入错误,请重新输入")}
var computerChoice = (Math.floor(Math.random()*3) + 3) * 10;
function compare(uChose, cChose){
var resule = uChose + cChose;
switch(resule){
case 30:case 43:case 56:
console.log("平手");
break;
case 36:case 40:case 53:
console.log("电脑赢了");
break;
case 33:case 46:case 50:
console.log("你赢了");
break;
}
}
compare(userChoice, computerChoice);