在此功能中,首先要求用户从rps数组[石头和剪刀]中选择一个,然后从数组中随机选择一个,然后为其分配变量计算机
然后找到两个选择的索引的索引,将这些索引隐式化并分配给变量梳,并传递给switch语句以评估并显示适当的msg,但在我的switch语句代码中,只有默认语句正在执行
请给我建议一个解决方案。
function RockPaperScissor() {
var rps = ["rock", "paper", "scissor"];
var player1 = prompt("Which one do you choose " + rps + "?");
var play1 = rps.indexOf(player1);
console.log("Player: " + play1);
var rand = Math.floor(Math.random() * rps.length);
console.log("computer: " + rand);
var computer = rps[rand];
var comb = play1.toString() + rand.toString();
console.log(comb);
if ((comb == '21') || (comb == '10') || (comb == '02')) {
alert("You choosed " + player1 + " and computer choosed " + computer + " Congrats!!! You Won");
}
else if ((comb == '12') || (comb == '01') || (comb == '20')) {
alert("You choosed " + player1 + " and computer choosed " + computer + " Sorry!!! You Lost");
}
else {
alert("You choosed " + player1 + " and computer choosed " + computer + " Match got tied");
}
// switch (comb) {
// case "21": alert("You choosed " + player1 + " and computer choosed " + computer + " Congrats!!! You Won");
// break;
// case "10": alert("You choosed " + player1 + " and computer choosed " + computer + " Congrats!!! You Won");
// break;
// case "02": alert("You choosed " + player1 + " and computer choosed " + computer + " Congrats!!! You Won");
// break;
// case "12": alert("You choosed " + player1 + " and computer choosed " + computer + " Sorry!!! You Lost");
// break;
// case "01": alert("You choosed " + player1 + " and computer choosed " + computer + " Sorry!!! You Lost");
// break;
// case "20": alert("You choosed " + player1 + " and computer choosed " + computer + " Sorry!!! You Lost");
// break;
// default: alert("You choosed " + player1 + " and computer choosed " + computer + " Match got tied");
// }
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Random Switch</title>
<script src="random_switch.js"></script>
</head>
<body>
<input type="button" value="Rock Paper Scissor" onclick="RockPaperScissor()">
</body>
</html>
答案 0 :(得分:0)
您只需要在开关case
中使用字符串值,因为comb
是String
类型而不是整数。
function RockPaperScissor() {
var rps = ["rock", "paper", "scissor"];
var player1 = prompt("Which one do you choose " + rps + "?");
var play1 = rps.indexOf(player1);
console.log("Player: " + play1);
var rand = Math.floor(Math.random() * rps.length);
console.log("computer: " + rand);
var computer = rps[rand];
var comb = play1.toString() + rand.toString();
console.log(comb);
switch (comb) {
case '21': alert("You choosed " + player1 + " and computer choosed " + computer + " Congrats!!! You Won");
break;
case '10': alert("You choosed " + player1 + " and computer choosed " + computer + " Congrats!!! You Won");
break;
case '02': alert("You choosed " + player1 + " and computer choosed " + computer + " Congrats!!! You Won");
break;
case '12': alert("You choosed " + player1 + " and computer choosed " + computer + " Sorry!!! You Lost");
break;
case '01': alert("You choosed " + player1 + " and computer choosed " + computer + " Sorry!!! You Lost");
break;
case '20': alert("You choosed " + player1 + " and computer choosed " + computer + " Sorry!!! You Lost");
break;
default: alert("You choosed " + player1 + " and computer choosed " + computer + " Match got tied");
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Random Switch</title>
<script src="random_switch.js"></script>
</head>
<body>
<input type="button" value="Rock Paper Scissor" onclick="RockPaperScissor()">
</body>
</html>
答案 1 :(得分:-1)
在这里,梳子是字符串类型,在我的情况下,我已经传递了一个数字,因此所有情况下仅执行默认填充都失败