gpt4 book ai didi

javascript - RPS 游戏应用程序无法运行

转载 作者:行者123 更新时间:2023-12-02 18:05:51 28 4
gpt4 key购买 nike

我正在开发一款 RPS 游戏,在设计风格之前,我在让它在基本级别上运行时遇到了一些麻烦。这是我的 HTML:

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="rpsfinal1.css">
<title>Shattered Template</title>
</head>
<body>
<div class="corners">
<h1 class="title">Rock Paper Scissors!</h1>
<p>Welcome to a Rock Paper Scissors web app!1
</p>
<script type="text/javascript" src="rps.js"></script>
<a href='linkhref.html' id='mylink'>Play Again</a>
<input type="button" value="Rock" onClick="rockChoice()">
<input type="button" value="Paper" onclick="paperChoice()">
<input type="button" value="Scissors" onClick="scissorsChoice()">
<p id="output"></p>
</div>
</body>
</html>

这是我的 JS:

var compare = function(choice1,choice2) {
if (choice1 === "rock") {
if (choice2 === "rock") {
console.log("It is a tie! The computer chose rock!");
alert ("It is a tie!");
}
else if (choice2 === "paper") {
console.log ("Sorry, you loose. :(");
alert ("Sorry, you lose. :( The computer chose paper!");
}
else {
console.log ("You win!");
alert ("You win! The computer chose scissors!");
}
}
else if (choice1 === "paper") {
if (choice2 === "rock") {
console.log ("You win!");
alert ("You win! The computer chose rock!");
}
else if (choice2 === "paper") {
console.log ("It is a tie!");
alert ("It is a tie! The computer chose paper!");
}
else {
console.log("Sorry, you lose. :(");
alert ("Sorry, you lose. :( The computer chose scissors!");
}
}
else {
if (choice2 === "rock") {
console.log ("You win!");
alert ("You win! The computer chose rock!");
}
else if (choice2 === "paper") {
console.log ("Sorry, you lose. :(");
alert ("Sorry, you lose. :( The computer chose paper!");
}
else {
console.log ("It is a tie!");
alert ("It is a tie! The computer chose scissors!");
}
}
};
var rockChoice = function() {
var userChoice = "rock";

var computerChoice = Math.random();
console.log(computerChoice);
if (computerChoice >= 0 && computerChoice <= 0.33) {
computerChoice = "rock";
console.log(computerChoice);
}
else if (computerChoice >= 0.34 && computerChoice <= 0.66) {
computerChoice = "paper";
console.log(computerChoice);
}
else {
computerChoice = "scissors";
console.log(computerChoice);
}

compare(userChoice,computerChoice);
}
var paperChoice = function() {
var userChoice = "paper";

var computerChoice = Math.random();
console.log(computerChoice);
if (computerChoice >= 0 && computerChoice <= 0.33) {
computerChoice = "rock";
console.log(computerChoice);
}
else if (computerChoice >= 0.34 && computerChoice <= 0.66) {
computerChoice = "paper";
console.log(computerChoice);
}
else {
computerChoice = "scissors";
console.log(computerChoice);
}

compare(userChoice,computerChoice);
}
var scissorsChoice = function() {
var userChoice = "scissors";

var computerChoice = Math.random();
console.log(computerChoice);
if (computerChoice >= 0 && computerChoice <= 0.33) {
computerChoice = "rock";
console.log(computerChoice);
}
else if (computerChoice >= 0.34 && computerChoice <= 0.66) {
computerChoice = "paper";
console.log(computerChoice);
}
else {
computerChoice = "scissors";
console.log(computerChoice);
}

compare(userChoice,computerChoice);
}

我认为代码无法工作的原因是我可能没有正确调用函数。请告诉我。谢谢!

最佳答案

我知道有点晚了,但对于那些正在寻找同样东西的人来说:- 您可能会收到“语法错误”,因为 JS 文件上的脚本末尾有一个额外的结束括号 ( } )。

关于javascript - RPS 游戏应用程序无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20124258/

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