好的,所以这应该很容易回答,但由于某种原因,我有一个问题。(可能是因为我非常新的编程)我已经创建了一个针对CPU的摇滚,纸张,剪刀游戏。现在我想询问用户是否想要再次播放,如果他们回答Y,那么每次都要经过循环。如果他们输入" N"然后游戏将结束。我遇到的主要问题是,一旦你输入Y再次玩,它就会给你上一场比赛的结果。任何指针都会有所帮助。
这就是我所拥有的:
var userChoice = "";
var userChoice = prompt("Choose rock, paper, or scissors");
var playagain = "Y";
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if (computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
choice1 = userChoice;
choice2 = computerChoice;
while (playagain == "Y") {
function compare(choice1, choice2) {
if (choice1 == choice2) {
return ("It's a tie!");
}
if (choice1 == "rock") {
if (choice2 == "scissors") {
return ("You win!");
} else {
return ("computer wins!");
}
}
if (choice1 == "paper") {
if (choice2 == "rock") {
return ("you win!");
} else {
return ("computer wins!");
}
}
if (choice1 == "scissors") {
if (choice2 == "rock") {
return ("computer wins!");
} else {
return ("you win!");
}
}
}
document.write(compare(choice1, choice2));
document.write("<br>");
playagain = prompt("Do you want to play again, Y or N");
userChoice = prompt("Choose rock, paper, or scissors");
}
答案 0 :(得分:2)
您唯一需要的是在while
循环下移动所有游戏逻辑:
function compare(choice1, choice2) {
if (choice1 == choice2) {
return ("It's a tie!");
}
if (choice1 == "rock") {
if (choice2 == "scissors") {
return ("You win!");
} else {
return ("computer wins!");
}
}
if (choice1 == "paper") {
if (choice2 == "rock") {
return ("you win!");
} else {
return ("computer wins!");
}
}
if (choice1 == "scissors") {
if (choice2 == "rock") {
return ("computer wins!");
} else {
return ("you win!");
}
}
}
}
var playagain = "Y";
while (playagain == "Y") {
var userChoice = prompt("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";
}
document.write(compare(userChoice, computerChoice));
document.write("<br>");
playagain = prompt("Do you want to play again, Y or N");
}
我有:
playagain
声明之外的所有内容移至循环choice1
和choice2
个本地变量,因为它们是多余的userChoice
声明答案 1 :(得分:0)
在while循环内部,您需要包含用户提示和新计算机生成的随机数。问题是你的循环一遍又一遍地计算相同的随机数和用户输入。
答案 2 :(得分:0)
很难准确解释什么是错误的,因为代码很难遵循。
你需要的是一个运行游戏的函数和一个调用该函数的while循环。
我正在打电话,这是未经测试的
function runGame() {
var userChoice = prompt("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";
}
if(userChoice==computerChoice){
return "It's a tie!";
}
if(userChoice=="rock"){
if(computerChoice=="scissors"){
return "You win!";
} else{
return "computer wins!";
}
}
if(userChoice=="paper"){
if(computerChoice=="rock"){
return "you win!";
} else{
return "computer wins!";
}
}
if(userChoice=="scissors"){
if(computerChoice=="rock"){
return "computer wins!" ;
} else{
return "you win!" ;
}
}
}
alert(runGame());
while ( prompt("Do you want to play again, Y or N") == "Y") {
alert(runGame());
}
答案 3 :(得分:0)
稍后凭借经验和知识,您可以编写如下脚本:
function compare(choice1, choice2) {
if (choice1 == choice2) {
return ("It's a tie!");
}
var lose = {
"rock": "scissors",
"paper": "rock",
"scissors": "paper"
};
return (lose[choice1] == choice2) ? "You win!" : "computer wins!";
}
(function() {
var CHOICE = ["rock", "paper", "scissors"];
var userChoice = "";
var userChoice, computerChoice, playagain, i = 0;
do {
document.write("Ground " + i + " :<br>");
computerChoice = CHOICE[Math.floor(Math.random() * 3)];
do {
userChoice = (""+prompt("Choose rock, paper, or scissors")).toLowerCase();
} while (CHOICE.indexOf(userChoice) === -1);
document.write(userChoice + " vs " + computerChoice + "<br>");
document.write(compare(userChoice, computerChoice) + "<br>");
playagain = (""+prompt("Do you want to play again, Y or N")).toUpperCase();
i++;
} while (["Y", "YES"].indexOf(playagain) !== -1);
});
我添加toLowerCase()
功能来替换&#34; Rock&#34; to&#34; rock&#34;,我添加toUpperCase()
来替换&#34; y&#34;到&#34; Y&#34;。
在这种情况下do { /* script */ } while (bool)
更合适。
使用:
do {
userChoice = (""+prompt("Choose rock, paper, or scissors")).toLowerCase();
} while (CHOICE.indexOf(userChoice) === -1);
您等待有效答案,仅限三个字:摇滚或纸张或剪刀。当userChoice等于&#34; rock&#34; indexOf
返回0;等于&#34;纸张#34; indexOf
返回1 ...
此语法:
return (lose[choice1] == choice2) ? "You win!" : "computer wins!";
是:
if (lose[choice1] == choice2)
return "You win!";
else
return "computer wins!"