亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

用于循環的 Java 腳本

用于循環的 Java 腳本

慕標琳琳 2022-10-13 10:53:40
我希望循環運行直到提示的值,但它會無限。需要幫忙。這是我的 javascript 代碼: 我的代碼var round = prompt("enter text");var roundno = round;var images_arr = ["../img/paper.png", "../img/stone.png", "../img/sisor.png"];var size = images_arr.lengthfunction myFunction() {for (var i = 0; i = roundno; i + 1) {    console.log(i);    setInterval(function () {        var x = Math.floor(size * Math.random())        $('#random').attr('src', images_arr[x]);    }, 1500);    setInterval(function () {        var sound = new Audio("../audio/audio.mp3");        sound.play();    }, 3000);    if (i = roundno) {        break;     }    }  }
查看完整描述

2 回答

?
長風秋雁

TA貢獻1757條經驗 獲得超7個贊

你的代碼有很多問題。


var round = prompt("enter text"); // how do you know the user enters a number?

var defaultNumberOfRounds = 1;  // I added this row


// CHANGED THIS IN EDIT 

var roundno = round; // should be: isNaN(Number(round)) ? defaultNumberOfRounds : round


var images_arr = ["../img/paper.png", "../img/stone.png", "../img/sisor.png"];

var size = images_arr.length

function myFunction() {


// CHANGED THIS IN EDIT the conditions within () should be: var i = 0; i < roundno; i++

for (var i = 0; i = roundno; i + 1) {

    console.log(i);


    // all iterations in the loop will execute this at the same time.

    setInterval(function () {

        var x = Math.floor(size * Math.random())

        $('#random').attr('src', images_arr[x]); // JQuery

    }, 1500);


    // all iterations in the loop will execute this at the same time.

    setInterval(function () {

        var sound = new Audio("../audio/audio.mp3");

        sound.play();

    }, 3000);


    if (i = roundno) { // should be i == roundno

        break; // don't need to break it, because your for loop's condition should take care of this

     }

    }

  }

} // ADDED THIS IN EDIT: missing curly bracket

[編輯] 我添加了一個片段來顯示我的代碼正在運行。我注釋了 for 循環中的所有代碼,我不得不更改roundnofor 循環中的聲明和語句。


var round = prompt("enter text");

var defaultNumberOfRounds = 1;

var roundno = isNaN(Number(round)) ? defaultNumberOfRounds : round;

var images_arr = ["../img/paper.png", "../img/stone.png", "../img/sisor.png"];

var size = images_arr.length;

console.log(`round: ${round}, roundno: ${roundno}`);

function myFunction() {

  for (var i = 0; i < roundno; i++) {

    console.log(i);


    /*setInterval(function () {

        var x = Math.floor(size * Math.random())

        $('#random').attr('src', images_arr[x]); // JQuery

    }, 1500);


    setInterval(function () {

        var sound = new Audio("../audio/audio.mp3");

        sound.play();

    }, 3000);*/


  }

}


myFunction();


查看完整回答
反對 回復 2022-10-13
?
有只小跳蛙

TA貢獻1824條經驗 獲得超8個贊

你沒有增加i

改變

i + 1

到以下之一:

  • i++

  • i += 1

  • i = i + 1

您還需要if在循環結束時修復語句。您要檢查是否i等于roundno。而不是使用賦值運算符來分配to=的值,使用or進行相等性檢查。roundnoi=====

if (i == roundno) { 
  break;
  }

您在循環條件中也犯了同樣的錯字。將循環條件更改為i == roundno并刪除if循環結束時的語句,因為循環條件固定,if循環內的語句是不必要的。

for (var i = 0; i == roundno; i++) { 
  // code
  }


查看完整回答
反對 回復 2022-10-13
  • 2 回答
  • 0 關注
  • 161 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號