0
var target=10;
var totalGuess=0;
var input = window.prompt('Enter a number between 1 and 100: ');  
var guess = parseInt(input);  
  do  
    {  
        totalGuess++;  
        if (guess < target)  
        {  
            window.prompt("Too low! Guess Again!");  
        }  
        if (guess > target)  
        {  
            window.prompt("Too High! Guess Again!");  
        }  
        if (guess == target)  
        {  
            alert("Thats right! It took you " +totalGuess +" tries to get it right!"); 
            break;
        }  

    } while (guess != target );  

我正在尝试检查用户输入的数字是高还是低,但该程序仅检查一个语句然后停止。

4

4 回答 4

1

您需要移动这两行:

var input = window.prompt('Enter a number between 1 and 100: ');  
var guess = parseInt(input);

到 do 语句的内部,以便反复提示用户,直到他们猜对为止。

于 2012-12-13T04:33:04.003 回答
0
var target=10;
var totalGuess=0;
var input = window.prompt('Enter a number between 1 and 100: ');  
var guess = parseInt(input);  
do  
    {  

        guess = parseInt(input);  
        totalGuess++;  

        if (guess < target)  
        {  
           input = window.prompt("Too low! Guess Again!");  
        }  
        if (guess > target)  
        {  
           input = window.prompt("Too High! Guess Again!");  
        }  
        if (guess == target)  
        {  
            alert("Thats right! It took you " +totalGuess +" tries to get it right!"); 
        break;
        }  


    } while (guess != target );  
于 2012-12-13T04:34:14.107 回答
0

试试这个

检查工作在这里http://jsfiddle.net/9mQ2b/14/

您没有重新初始化 Guess 变量,以便它每次都可以检查条件

<!DOCTYPE html>
<html>
<body>
<script>

var target=10;
var totalGuess=0;
var input = window.prompt('Enter a number between 1 and 100: ');  
var guess = parseInt(input);  
do  
    {  


        totalGuess++;  

        if (guess < target)  
        {  
 guess = parseInt (window.prompt("Too low! Guess Again!"));  
        }  
        if (guess > target)  
        {  
 guess = parseInt (window.prompt("Too High! Guess Again!"));  
        }  
        if (guess == target)  
        {  
            alert("Thats right! It took you " +totalGuess +" tries to get it right!"); 
        break;
        }  


    } while (guess != target ); 
</script>
</body>
</html>
于 2012-12-13T04:37:33.167 回答
0

问题只是您的第一个输入被输入到程序中。您以后的输入值将被忽略。尝试这个。

    if (guess < target)  
        {  
                input = window.prompt("Too low! Guess Again!");
                guess = parseInt(input);   
        }  
        if (guess > target)  
        {  
                input = window.prompt("Too High! Guess Again!"); 
                guess = parseInt(input); 
        }  
        if (guess == target)  
        {  
                alert("Thats right! It took you " +totalGuess +" tries to get it right!"); 
        break;
  }
于 2012-12-13T04:40:21.417 回答