2011-04-12 6 views
0

저는 "Rock Paper Scissors"게임을 통해 JavaScript의 기본 사항을 이해하고 있는지 확인하고 있습니다. fight()의 30 번째 줄부터 71 번째 줄까지 if 문을 사용할 때까지는 모두 잘 작동합니다. 응답 변수를 조정하여 "if'ing"이 될 수 있음을 나타냅니다. 전반적인 "if"에서 모든 하위 "if"의 기본값을 반환하는 것으로 보입니다. 어쩌면 보조 변수에 잘못된 변수를 사용하고 있습니까?스위치 문을 잘못 포맷합니까?

radioGroup.html이

<script type = "text/javascript"> 
     //<![CDATA[ 
     // from radioGroup.html 
     function fight(){ 
     var randomChoice = Math.random(); 
     var threeChoice = Math.floor (randomChoice * 3); 
     var weapon = document.getElementsByName("weapon"); 

     for (i = 0; i < weapon.length; i++){ 
      currentWeapon = weapon[i]; 

      if (currentWeapon.checked){ 
      var selectedWeapon = currentWeapon.value; 
      } // end if 

     } // end for 

     var cpuChoice = new Array("Paper", "Rock", "Scissors") 
     var reply = "xxx" 
     if (threeChoice === 0) { 
      if (weapon === "Paper") { 
       reply = "11"; 
       } 
      else if (weapon === "Rock") { 
        reply = "12"; 
       } 
      else if (weapon === "Scissors") { 
        reply = "13"; 
        } 
      else { 
        reply = "what1"; 
        } 
     }else if (threeChoice === 1) { 
       if (weapon === "Paper") { 
       reply = "21"; 
       } 
      else if (weapon === "Rock") { 
        reply = "22"; 
       } 
      else if (weapon === "Scissors") { 
        reply = "23"; 
        } 
      else { 
        reply = "what2"; 
        } 
     }else if (threeChoice === 2) { 
       if (weapon === "Paper") { 
       reply = "31"; 
       } 
      else if (weapon === "Rock") { 
        reply = "32"; 
       } 
      else if (weapon === "Scissors") { 
        reply = "33"; 
        } 
      else { 
        reply = "what3"; 
        } 
     }else { 
      reply = "hay now?"; 
      } 
     var output = document.getElementById("output"); 
     var response = "<h2>You have chosen "; 
     response += selectedWeapon + "<h2>The CPU has chosen "; 
     "<\/h2> \n"; 
     response += cpuChoice[threeChoice] + "<\/h2> \n"; 
     response += reply + "<\/h2> \n"; 
     output.innerHTML = response; 
    } // end function 

     //]]> 
    </script> 
    </head> 

    <body> 
    <h1>Choose!</h1> 
    <form action = ""> 
     <fieldset> 
     <input type = "radio" 
       name = "weapon" 
       id = "radPaper" 
       value = "Paper" 
       checked = "checked" /> 
     <label for = "radPaper">Paper</label> 

     <input type = "radio" 
       name = "weapon" 
       id = "radRock" 
       value = "Rock" /> 
     <label for = "radRock">Rock</label> 

     <input type = "radio" 
       name = "weapon" 
       id = "radScissors" 
       value = "Scissors" /> 
     <label for = "radScissors">Scissors</label> 
     <button type = "button" 
       onclick = "fight()"> 
      fight the dragon 
     </button> 
     </fieldset> 
    </form> 
    <div id = "output"> 

    </div> 
    </body> 
</html> 
+0

달려 있습니다. 비교 변수가 항상 같습니까? "(weapon ==="Paper ") && (threeChoice === 0)"? == "(weapon ==== 'Paper') && (threeChoice === 0)"{/ * do this/*} –

답변

0

잘못 스위치 상태를 사용하는 방법에 대한 올바른은 "사건"스위치의 표현에 평가하는 것이다. 문 경우 사용되어야하지만이

switch (weapon === "paper") { 
case true: 
    //do something 
    break; 
case false: 
    // do something else 
default: 
    // oh no! it wasn't true of false 
} 
+0

그럼 당신은 저에게 if else를 시도 할 것을 확신했습니다. 대신에 나는 아직 첫 번째 조건을 얻지 못했다고 생각한다. \t 'code'if ((weapon === "Paper") && (threeChoice === 0)) { \t \t \t reply = " 타이 "(용지") && (threeChoice는 === 1) { \t \t \t 응답은 = 종이 락 커버 \t \t은} (무기 ===)는 "다른 경우" ";! \t \t을} 다른 경우 ((무기 === "종이") && 세 선택 === 2) { \t \t \t reply = "종이가 가위로 잘립니다"; \t \t} else { \t \t \t reply = "hay now?"; \t \t \t} '코드' – TurtleWolf

0

case 같은 스위치 문을 사용할 수 있습니다

은 일반적으로 숫자 간단한 값을 테스트하기위한 것입니다. 귀하의 경우, 바위, 종이 및 가위에 숫자를 할당하여 해당 숫자 순서에 따라 누가 누가 이겼는지 분명하게 알 수 있습니다. 예를 들어 rock = 0, paper = 1, scissors = 2입니다. 그러면 (3 + (a - b)) % 3는 누가 이겼는지 알려줍니다. 0은 동점, 1은 a, 2는 b입니다. 그러면 해당 작업의 결과에 대해 간단한 3 가지 경우 switch을 수행 할 수 있습니다.

관련 문제