我尝试用JS制作石头剪刀。我希望它能够计算积分并宣布获胜者。但是我的代码很乱。计算机总是赢,总是打印丢失。我真的不知道问题是什么。我也试图做到这一点,以便在每一轮之后,你可以做出新的选择,但这并没有按计划完成。
var pcChoice = ["Steen", "Papier", "Schaar"];
var userChoice = prompt("Steen, papier of schaar?");
var pcWins = 0;
var userWins = 0;
var totalWins = pcWins + userWins
var win = ("Je hebt de ronde gewonnen!")
var Verlies = ("Je hebt verloren")
function randomNumber() {
return (Math.floor(Math.random() * 3));
}
console.log("Computer koos: " + pcChoice[randomNumber()]);
console.log("Jij koos: " + userChoice);
if (pcWins + userWins === 2) {
prompt("Nieuwe keuze")
}
if (pcWins + userWins === 1) {
prompt("Nieuwe keuze")
}
while (pcWins + userWins < 3) {
if (userChoice === pcChoice) {
console.log("Gelijkspel");
} else if (userChoice === "Steen" && pcChoice === "Schaar") {
console.log(win) + userWins++
} else if (userChoice === "Papier" && pcChoice === "Steen") {
console.log(win) + userWins++
} else if (userChoice === "Schaar" && pcChoice === "Papier") {
console.log(win) + userWins++
} else {
console.log(Verlies) + pcWins++
}
}
// Het volgende zal de winner uitprinten
if (pcWins > userWins) {
console.log("De computer wint!")
} else {
console.log("Je hebt gewonnen")
}
答案 0 :(得分:0)
很多拼写错误和初学者错误。
我已在下面的代码段中清理了您的代码。
始终确保您的While循环不会无限循环!
var pcChoices = [
'Steen',
'Papier',
'Schaar'
];
var userChoice = prompt('Steen, papier of schaar?');
var pcWins = 0;
var userWins = 0;
var totalWins = pcWins + userWins;
var win = ('Je hebt de ronde gewonnen!');
var Verlies = ('Je hebt verloren');
function randomNumber() {
return (Math.floor(Math.random() * 2));
}
console.log('Computer koos: ' + pcChoices[randomNumber()]);
console.log('Jij koos: ' + userChoice);
if (pcWins + userWins === 2) {
prompt('Nieuwe keuze');
}
if (pcWins + userWins === 1) {
prompt('Nieuwe keuze');
}
while (pcWins + userWins < 3) {
var pcChoice = pcChoices[randomNumber()];
if (userChoice === pcChoice) {
console.log('Gelijkspel');
break;
}
else if (userChoice === 'Steen' && pcChoice === 'Schaar') {
console.log(win);
userWins++;
}
else if (userChoice === 'Papier' && pcChoice === 'Steen') {
console.log(win);
userWins++;
}
else if (userChoice === 'Schaar' && pcChoice === 'Papier') {
console.log(win);
userWins++;
}
else {
console.log(Verlies);
pcWins++;
}
} // Het volgende zal de winner uitprinten
if (pcWins > userWins) {
console.log('De computer wint!');
}
else {
console.log('Je hebt gewonnen');
}
&#13;
答案 1 :(得分:0)
我希望这会有所帮助。 它一次需要一轮而不是一次完成。通过查找一个小表来计算结果或一轮。你的计算胜利的版本没有理由不起作用。
请原谅我的荷兰人。
var choice = {STEEN: 0, PAPIER: 1, SCHAAR: 2};
var resultMatrix = [[0, -1, 1], [1, 0, -1], [-1, 1, 0]];
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function playRound(round){
var userInput = prompt("Round " + round + ": steen, papier of schaar?");
var userChoice = choice[userInput.toUpperCase()];
var pcInput = Object.keys(choice)[getRandomInt(0,2)];
var pcChoice = choice[pcInput];
var result = resultMatrix[userChoice][pcChoice];
console.log("Ronde " + round);
console.log("Jij koos: " + userInput.toUpperCase());
console.log("Computer koos: " + pcInput);
switch(result){
case -1:
console.log("De computer wint!");
break;
case 0:
console.log("Gelijkspel");
break;
case 1:
console.log("Je hebt gewonnen");
break;
}
console.log(" ");
return result
};
var totalUserWins = 0;
for(var i=1; i<=3; i++){ totalUserWins += playRound(i); }
console.log("Laatste");
if (totalUserWins > 0) { console.log("Je hebt gewonnen"); } else
if (totalUserWins === 0) { console.log("Het is een gelijkspel"); } else
if (totalUserWins < 0) { console.log("De computer wint!"); }
&#13;