Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将段落、换行符或水平规则标记添加到此代码中?_Javascript - Fatal编程技术网

Javascript 如何将段落、换行符或水平规则标记添加到此代码中?

Javascript 如何将段落、换行符或水平规则标记添加到此代码中?,javascript,Javascript,即使添加了所需的标记,也会出现错误 请添加任何用于留下行或div标记的标记。如果可能,请建议一些改进。您的代码出现在错误警报中的原因很可能是因为您在比较文本时在文本中添加了空格,例如If(userChoice=='Rock')以及您忽略了JavaScript区分大小写。意思是岩石摇滚乐“ 我将尽可能地使用未更改的代码来确保它仍然是您的代码,但请注意,有许多更好的方法来编写它,而不是比较字符串值 使用toUpperCase()可以比较字符串,无论用户类型是rock还是rock 您可能还可以添加一个

即使添加了所需的标记,也会出现错误


请添加任何用于留下行或div标记的标记。如果可能,请建议一些改进。

您的代码出现在错误警报中的原因很可能是因为您在比较文本时在文本中添加了空格,例如
If(userChoice=='Rock')
以及您忽略了JavaScript区分大小写。意思是岩石摇滚乐“

我将尽可能地使用未更改的代码来确保它仍然是您的代码,但请注意,有许多更好的方法来编写它,而不是比较字符串值

使用
toUpperCase()
可以比较字符串,无论用户类型是
rock
还是
rock

您可能还可以添加一个
trim()
,因为用户可以选择带有空格的
Rock
。考虑将用户选择更改为下拉以消除该问题。

关于换行符,由于您没有具体说明位置,我假设您的意思是在计算机选择的文本和结果之间

您可以通过添加一个

来添加换行符,例如
document.write(“
Computer wins!!!”

请注意,正如其他人所说,使用
文档。编写
并不好,
请改为尝试使用
document.createElement()
。同时,考虑 将用户选择更改为下拉列表以消除字符串问题。你 可以更好地验证选项的数字表示形式,而不是 比较字符串。不过我把研究留给你了

无论如何,下面的代码尽可能接近您的代码,只需进行最少的更改即可使其与预期结果匹配并修复错误

var userChoice=prompt('在石头、布或剪刀之间选择');
var computerChoiceArray=[‘石头’、‘布’、‘剪刀’];
var choiceNumber=Math.floor(Math.random()*3);
var compChoice=computerChoiceArray[choiceNumber];
文件。写入('计算机选择'+计算机选择');
var game=函数(compChoice,userChoice){
if(compChoice.toUpperCase()==userChoice.toUpperCase()){
document.write(“
结果是平局!!”) }else if(compChoice.toUpperCase()===“ROCK”&&userChoice.toUpperCase()===“剪刀”){ 文档。写(“
计算机赢!!!”) }else if(compChoice.toUpperCase()==“ROCK”&&userChoice.toUpperCase()==“PAPER”){ 文档。写(“
你赢了!!!”) }else if(compChoice.toUpperCase()=“剪刀”和&userChoice.toUpperCase()=“石头”){ 文档。写(“
你赢了!!!”) }else if(compChoice.toUpperCase()=“剪刀”&&userChoice.toUpperCase()=“纸”){ 文档。写(“
计算机赢!!!”) }else if(compChoice.toUpperCase()==“纸张”&&userChoice.toUpperCase()==“岩石”){ 文档。写(“
计算机赢!!!”) }else if(compChoice.toUpperCase()==“纸”和&userChoice.toUpperCase()===“剪刀”){ 文档。写(“
你赢了!!!”) }否则{ 警报(“错误,+userChoice+”不适用); } }
游戏(compChoice,userChoice)我已更新了您的代码。我想展示的主要内容是,必须将代码拆分为逻辑部分。在我的例子中,有:

  • 对象
    游戏
    存储所有信息
  • 函数
    round
    启动新一轮并编写游戏日志
  • 显示进度的函数
    writeLogs
请注意,这不是实现此游戏的最佳方式

var computerChoiceArray=['rock'、'paper'、'剪刀'];
var最大循环次数=3;
var博弈={
doElement:document.getElementById('game-wrapper'),
圆形中心:0,
日志:[]
};
功能回合(游戏){
如果(game.roundsconter>=最大回合数){
game.log.push(“结束”);
写日志(游戏);
返回;
}
var userChoice=prompt('Choose from:'+computerChoiceArray.join(','))| |';
userChoice=userChoice.toLowerCase();
var compChoiceNumber=Math.floor(Math.random()*3);
var compChoice=computerChoiceArray[compChoiceNumber];
if(compChoice==userChoice){
game.logs.push(“结果是平局!!”);
}否则如果(compChoice==“岩石”和&userChoice==“剪刀”){
game.log.push(“电脑赢!!!”);
}否则如果(compChoice==“rock”&&userChoice==“paper”){
game.logs.push(“你赢了!!!”);
}否则如果(compChoice==“剪刀”和&userChoice==“石头”){
game.logs.push(“你赢了!!!”);
}else if(compChoice==“剪刀”和&userChoice==“纸”){
game.log.push(“电脑赢!!!”);
}否则如果(compChoice==“纸张”&&userChoice==“岩石”){
game.log.push(“电脑赢!!!”);
}else if(compChoice==“纸”和&userChoice==“剪刀”){
game.logs.push(“你赢了!!!”);
}否则{
game.logs.push(“错误,+userChoice+”不适用);
}
game.RoundSconter+=1;
写日志(游戏);
//请等待一秒钟以写入日志并开始新一轮
setTimeout(函数(){
回合(游戏);
}, 1000)
}
函数写日志(游戏){
//清除日志
game.doElement.innerHTML='';
for(game.logs的var日志){
var logHTMLElement=document.createElement('div');
logHTMLElement.innerHTML=log;
game.DOMElement.appendChild(logHTMLElement);
}
}
//开始第一轮
回合(游戏)

我在代码中没有看到您试图编写任何

的地方。。。?此外,您可能会发现正确的DOM操作比
文档更容易。编写
我想打印计算机的选择,并在单独的行上显示结果您的代码是一个无限循环的警报框垃圾邮件:)@不,您别无选择。就像Joomanji-你必须在开始后完成游戏:-)P.s.我已经移除了循环)
var userChoice = prompt('Choose between Rock,Paper or Scissors');
var computerChoiceArray = ['Rock','Paper','Scissors'];
var choiceNumber = Math.floor(Math.random()*3);

var compChoice  = computerChoiceArray[choiceNumber];
document.write('Computer chose '+ compChoice)    
    var game = function(compChoice,userChoice) {
        if (compChoice === userChoice) {
            document.write("The result is a Tie!!")
        }else if( compChoice === "Rock " && userChoice === "Scissors") {
            document.write("Computer wins !!!")
        }else if( compChoice === "Rock " && userChoice === "Paper") {
            document.write("You won !!!")
        }else if( compChoice === "Scissors" && userChoice === "Rock") {
            document.write ("You won !!!")
        }else if( compChoice === "Scissors" && userChoice === "Paper") {
            document.write("Computer wins !!!")
        }else if( compChoice === "Paper" && userChoice === "Rock") {
            document.write("Computer wins !!!")
        }else if( compChoice === "Paper" && userChoice === "Scissors") {
            document.write ("You won !!!")
        }else{
            alert ("Error, "+userChoice+" is not applicable ");
        }
    }

 game(compChoice,userChoice);