无法保存JavaScript prompt()函数

时间:2015-08-12 22:05:30

标签: javascript html prompt

我是JavaScript的新手,我正在构建一个非常基本的战舰计划,但我注意到,如果提示()函数似乎没有显示对话框,我尝试将它的值保存到变量中。这是我的代码:

var location1 = 3;
var location2 = 4;
var location3 = 5;


var hits = 0;
var guesses = 0;
var guess;

var isSunk = false;


while (isSunk == false){
guess = prompt("Ready, aim, fire! Enter a number 0-6");
if (guess < 0 || > 6){
    alert("PLease enter a valid cell number!");
}
else{
    guesses = guesses + 1;

    if (guess == location1 || guess == location2 || guess == location3){
        alert("Hit!");
        hits = hits + 1;
        if (hits == 3){
            isSunk = true;
            alert("You sank my battleship!");
        }
        else{
            alert("Miss!");
        }
    }
}
}
var stats = "You took " + guesses + " guesses to sink the battleship, " + "which means your shooting accuracy was " + (3/guesses);
alert(stats);

现在,当我加载浏览器时,我认为我的提示没有出现的原因是因为我试图将提示的值设置为变量猜测是因为以下代码执行时没有问题:

 var location1 = 3;
 var location2 = 4;
 var location3 = 5;

 var hits = 0;
 var guesses = 0;
 var guess;

 var isSunk = false;


 while (isSunk == false){
   prompt("Ready, aim, fire! Enter a number 0-6");
   isSunk = true;
}
var stats = "You took " + guesses + " guesses to sink the battleship, " + "which means your shooting accuracy was " + (3/guesses);
alert(stats);

有人可以帮我理解我的代码问题是什么吗?在此先感谢:)

2 个答案:

答案 0 :(得分:3)

您正确使用prompt() 问题出在下面一行:
if (guess < 0 || guess > 6){
应该是 prompt()

此错误在调用Ctrl+Shift+K之前停止了JS。 将来,您应该使用浏览器控制台来调试代码。你会看到这个错误。
如果您使用Firefox,则可以使用Ctrl+Shift+J打开它。 Chrome使用{{1}}。

如果你已经解决了这个问题,你会发现其余代码没有语法错误,但没有做正确的事情(例如我可以多次访问同一个位置)。逻辑错误不容易修复(您没有调试器可以帮助您)。祝你好运! 好吧,在你的情况下不要担心它不会很难解决。

答案 1 :(得分:1)

+1以上评论。

此外,Kiyana,当您运行代码时,您会注意到只要用户的输入不是&lt; 0或&gt; 6,警报(“Hit!”)和警报(“Miss!”)都出现了。

这与代码的排序有关:

if (guess == location1 || guess == location2 || guess == location3){
    alert("Hit!");
    hits = hits + 1;
    if (hits == 3){
        isSunk = true;
        alert("You sank my battleship!");
    }
    else{
        alert("Miss!");
    }
}

以下是您希望上述代码显示的顺序。

伪代码:

  1. 如果用户的猜测是3,4或5(location1,location2或location3),那么我们要提醒“点击”

  2. 然后,在命中变量上添加1个计数器(hit = hit + 1)

  3. ELSE - 然后我们要提醒“小姐。”

  4. 评估此猜测后,我们想检查点击计数器是否达到3。

  5. a)如果没有,那么我们循环回所有代码。

    b)如果有,那么我们想让isSunk等于true(停止循环)并警告“你沉没了我的战舰!”

    代码应如下所示:

    var location1 = 3;
    var location2 = 4;
    var location3 = 5;
    
    
    var hits = 0;
    var guesses = 0;
    var guess;
    
    var isSunk = false;
    
    
    while (isSunk === false){
    guess = prompt("Ready, aim, fire! Enter a number 0-6");
    
    if (guess < 0 || guess > 6){
        alert("Please enter a valid cell number!");
    } else{
        guesses = guesses + 1;
    
    if (guess == location1 || guess == location2 || guess == location3){
        alert("Hit!");
        hits = hits + 1;
        }else{
            alert("Miss!");
        }
        if (hits == 3){
            isSunk = true;
            alert("You sank my battleship!");
        }
    }
    }
    var stats = "You took " + guesses + " guesses to sink the battleship, " +  "which means your shooting accuracy was " + (3/guesses);
    alert(stats);
    

    希望这有帮助!