对于与单人工作有关的任务,我必须使用JavaScript制作Rock,Paper,Scissors游戏。我们必须以变量为基础,并且必须使用while循环。
当计算机或用户赢得三场比赛时,游戏需要结束。在运行时,我在第48行发现错误,并且不知道如何解决它,也不确定如何正确实现循环。帮助将不胜感激。
var cpuWins = 0;
var userWins = 0;
var totalWins=0;
while(totalWins!=5){
var userChoice = parseInt(prompt("Enter 1 for Rock, 2 for Paper, 3 for Scissors"));
var cpuNumber = Math.random();
if (cpuNumber <= (1/3)){
cpuChoice = "Rock";
}
else if (cpuNumber <=(2/3)){
cpuChoice = "Paper";
}
else{
cpuChoice= "Scissors";
}
if (userChoice===1){
if (cpuChoice==="Scissors"){
userWins++;
}
else if(cpuChoice==="Paper"){
cpuWins++;
}
else{
alert("It was a tie");
}
}
if else(userChoice===2){
if (cpuChoice==="Rock"){
userWins++;
}
else if{
cpuWins++;
}
else{
alert("It was a tie");
}
}
else{
if (cpuChoice==="Paper"){
userWins++;
}
else if(cpuChoice==="Rock"){
cpuWins++;
}
else{
alert("It was a tie");
}
}
totalWins++
}
&#13;
答案 0 :(得分:0)
如上所述,你的else if语句只有一些小的语法错误。请参阅下面的代码:
var cpuWins = 0;
var userWins = 0;
var totalWins=0;
while(totalWins!=5){
var userChoice = parseInt(prompt("Enter 1 for Rock, 2 for Paper, 3 for Scissors"));
var cpuNumber = Math.random();
if (cpuNumber <= (1/3)){
cpuChoice = "Rock";
}
else if (cpuNumber <=(2/3)){
cpuChoice = "Paper";
}
else{
cpuChoice= "Scissors";
}
if (userChoice===1){
if (cpuChoice==="Scissors"){
userWins++;
}
else if(cpuChoice==="Paper"){
cpuWins++;
}
else{
alert("It was a tie");
}
}
else if (userChoice===2){
if (cpuChoice==="Rock"){
userWins++;
}
else if(cpuChoice==="Scissors"){
cpuWins++;
}
else{
alert("It was a tie");
}
}
else {
if (cpuChoice==="Paper"){
userWins++;
}
else if (cpuChoice==="Rock"){
cpuWins++;
}
else{
alert("It was a tie");
}
}
totalWins++
}
&#13;
答案 1 :(得分:0)
您遇到语法错误:
else if{
不正确,你需要在那里指定一个标准
if else
不正确,订单正确
else if
我只纠正了语法错误,你应该想出逻辑
var cpuWins = 0;
var userWins = 0;
var totalWins=0;
while(totalWins!=5){
var userChoice = parseInt(prompt("Enter 1 for Rock, 2 for Paper, 3 for Scissors"));
var cpuNumber = Math.random();
if (cpuNumber <= (1/3)){
cpuChoice = "Rock";
}
else if (cpuNumber <=(2/3)){
cpuChoice = "Paper";
}
else{
cpuChoice= "Scissors";
}
if (userChoice===1){
if (cpuChoice==="Scissors"){
userWins++;
}
else if(cpuChoice==="Paper"){
cpuWins++;
}
else{
alert("It was a tie");
}
}
else if(userChoice===2){
if (cpuChoice==="Rock"){
userWins++;
}
else if (cpuChoice ==="Scrissors"){
cpuWins++;
}
else{
alert("It was a tie");
}
}
else{
if (cpuChoice==="Paper"){
userWins++;
}
else if(cpuChoice==="Rock"){
cpuWins++;
}
else{
alert("It was a tie");
}
}
totalWins++
}