JavaScript中单个换行符的问题

时间:2011-02-28 12:34:23

标签: javascript

您好 我查看了与JavaScript相关的问题,但没有找到任何结果。我想要做的很简单;我知道怎么做,但由于某种原因,它不起作用。

以下是我遇到问题的代码:

playersScore = rollDie();
document.write('Score: ' + playersScore);
playersPosition = playersScore + playersPosition;
document.write(', Square: ' + playersPosition);

indexOfNumber = findIndexOf(playersPosition, specialSquaresArray);

if (indexOfNumber != -1) {
    document.write(', Ladder to Square: ' + connectedSquaresArray[indexOfNumber] + '<BR>');
    playersPosition = connectedSquaresArray[indexOfNumber];
    indexOfNumber = -1;
}
// end of question(iii) 
// start of question(iv)(b) 
while (playersPosition < 80) {
    playersScore = rollDie()
    document.write('Score: ' + playersScore)
    playersPosition = playersPosition + playersScore
    document.write(', Square: ' + playersPosition)
    indexOfNumber = findIndexOf(playersPosition, specialSquaresArray)
    if (indexOfNumber != -1) {
        document.write(', Ladder to Square: ' + connectedSquaresArray[indexOfNumber]);
        playersPosition = connectedSquaresArray[indexOfNumber];
    }
    document.write('<BR>');

这是浏览器中的结果:

Score: 4, Square: 4Score: 4, Square: 8
Score: 2, Square: 10
Score: 1, Square: 11
Score: 2, Square: 13
Score: 5, Square: 18
Score: 4, Square: 22
Score: 1, Square: 23
Score: 5, Square: 28
Score: 3, Square: 31
Score: 5, Square: 36
Score: 3, Square: 39, Ladder to Square: 51
Score: 4, Square: 55
Score: 6, Square: 61
Score: 6, Square: 67
Score: 1, Square: 68, Ladder to Square: 73
Score: 1, Square: 74
Score: 3, Square: 77, Ladder to Square: 58
Score: 5, Square: 63
Score: 1, Square: 64
Score: 4, Square: 68, Ladder to Square: 73
Score: 6, Square: 79
Score: 6, Square: 85

第一行是玩家的'第一次去'(得分:4,平方:4)但我需要紧随其后的线(得分:4,平方:8)才能突破到下面的线但是无论在哪里我放置<BR>它不会这样做。前几行是问题还是在while循环开始时出现问题?我真的想不出来!!

我真的很感激这方面的一些帮助。 非常感谢提前。

5 个答案:

答案 0 :(得分:3)

如果没有找到梯子,只需写下换行符:

if (indexOfNumber != -1) {
    document.write(', Ladder to Square: ' + connectedSquaresArray[indexOfNumber] + '<BR>');
    playersPosition = connectedSquaresArray[indexOfNumber];
    indexOfNumber = -1;
}
else {
   document.write('<BR>');
}

答案 1 :(得分:1)

将第4行更改为以下

document.write(', Square: ' + playersPosition + '<br/>');

答案 2 :(得分:0)

放另一个

     document.write('<BR>'); 

作为While Loop的第一行。

答案 3 :(得分:0)

您必须明确写出HTML和换行符,例如document.write('<br />');

答案 4 :(得分:0)

您需要添加

document.write('<BR>');
在你的while循环之前

在输出的第一部分之后断行。你确实有一个BR,但只有if测试通过才会输出。

从第一个'Ladder to Square:'输出行中删除BR,然后在while循环之前添加一个BR,以便始终输出。