gpt4 book ai didi

javascript - JavaScript 石头剪刀布打领带案例

转载 作者:行者123 更新时间:2023-11-28 15:40:27 25 4
gpt4 key购买 nike

我目前正在学习 CodeAcademy JS 类(class),他们告诉我尝试实现重试如果用户的选择和计算机的选择相同的情况。首先,请允许我为我的错误缩进道歉,因为我仍在学习如何正确识别我的代码。看一下 if(choice1 === choice2)。在它下面,我编写了执行重试情况的方法,但它失败了。我很高兴收到关于我应该如何操作的帮助如果选择之间存在平局,则创建重试案例?提前致谢!

var userChoice = prompt("Do you choose rock, paper or scissors?");
while (userChoice !=="rock" && userChoice !=="paper" && userChoice !=="scissors"){
confirm(userChoice + " isn't rock, paper or scissors. How about you try again?");
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("You Chose: "+userChoice);
console.log("The Computer Chose: "+computerChoice);
var compare = function (choice1,choice2){
if (choice1 === choice2) {
userChoice = prompt("Choose again");
computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
compare(userChoice,computerChoice);
}

if (choice1 === "rock"){
if (choice2 === "paper"){
return "Paper wins";
}
if (choice2 === "scissors") {
return "Rock wins";
}
}

if (choice1 === "paper"){
if (choice2 === "scissors"){
return "Scissors wins!";
}
if (choice2 === "rock"){
return "Paper wins!";
}
}

if (choice1 === "scissors"){
if (choice2 === "rock"){
return "Rock wins!";
}
if (choice2 === "paper"){
return "Scissors wins!";
}
}
};
compare(userChoice,computerChoice);

最佳答案

给你:

function rockpaperscissors() {
var userChoice = prompt("Do you choose rock, paper or scissors?");
while (userChoice !=="rock" && userChoice !=="paper" && userChoice !=="scissors"){
confirm(userChoice + " isn't rock, paper or scissors. How about you try again?");
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("You Chose: "+userChoice);
console.log("The Computer Chose: "+computerChoice);
var choice1 = userChoice;
var choice2 = computerChoice;
if (choice1 === choice2){
confirm('Sorry, but there was a tie. You and the computer are equals. Let\'s try again anyways.');
return rockpaperscissors();
}

if (choice1 === "rock"){
if (choice2 === "paper"){
return "Paper wins";
}
if (choice2 === "scissors") {
return "Rock wins";
}
}
if (choice1 === "paper"){
if (choice2 === "scissors"){
return "Scissors wins!";
}
if (choice2 === "rock"){
return "Paper wins!";
}
}
if (choice1 === "scissors"){
if (choice2 === "rock"){
return "Rock wins!";
}
if (choice2 === "paper"){
return "Scissors wins!";
}
}
}
confirm(rockpaperscissors());

现在,解释一下我做了什么:

  • 现在一切都位于rockpaperscissors 函数中。这样您就可以从内部调用它,而无需重复代码。
  • 用两个空格重新缩进所有内容。说真的,缩进很重要。
  • 如果出现平局,它会确认告诉您存在平局,然后重新启动。
  • 一旦达到非平局,它将确认您的结果。

另外,因为我觉得这样,一个非常精致的版本,使用了大量复杂的 JS:

function rockpaperscissors() {
var user = '';
do {
user = prompt((user!==''?'Invalid answer. ':'')+'Choose rock, paper, or scissors.');
} while (['rock', 'paper', 'scissors'].indexOf(user) == -1);
var computer = ['rock', 'paper', 'scissors'][Math.floor(Math.random()*3)];

switch (user+'|'+computer) {
case "rock|scissors":
case "paper|rock":
case "scissors|paper":
return "User Wins!";
case "rock|rock":
case "paper|paper":
case "scissors|scissors":
return rockpaperscissors();
default:
return "Computer Wins!";
}

}
confirm(rockpaperscissors());

关于javascript - JavaScript 石头剪刀布打领带案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23981459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com