簡體   English   中英

計數變量未遍歷for循環

[英]count variable not iterating through for loops

嘿,我每次嘗試通過if語句成功循環時,都會嘗試增加此變量num_pass的計數。 它一直吐出0或1。

我曾嘗試將變量放置在許多不同的地方並在不同的地方聲明它,但仍然沒有成功。

function pickup()
{   
var num_pass = 0;
var i;
var array = PASSENGERS[i];
for (var i = 0; i < PASSENGERS.length; i++)
{
    // get location of passengers
    var lat = PASSENGERS[i].placemark.getGeometry().getLatitude();
    var long = PASSENGERS[i].placemark.getGeometry().getLongitude();

    // calculate distance of passengers to bus
    var distance = shuttle.distance(lat, long);

    // screen for freshman

    // if passengers are close enough
    if (distance <= 15)
    {
        // if there is room on the bus
        // iterate through all the seats
        var j;
        var array = shuttle.seats;

  for (j = 0;j < shuttle.seats.length;j++)
        {

            // if a seat is empty
            if (shuttle.seats[j] == null && PASSENGERS[i].house != "Thayer Hall") 
            {          

                // remove picture from the 3-D map
                var features = earth.getFeatures();
                features.removeChild(PASSENGERS[i].placemark);

                // remove marker from the 2-D map
                PASSENGERS[i].marker.setMap(null);

                // remove 2-D map attribute of passenger
                PASSENGERS[i].marker = null;

                // add to the shuttle
                shuttle.seats[j] = PASSENGERS[i];

                // update the chart
                chart();

                $('#announcements').html("Passenger picked up!"); 
                $('#announcements').html("Score: " + score);

                num_pass++;                    


            }
            console.log(shuttle.seats[j]); 

        }


    }

    else if (num_pass > 9)
    {
        $('#announcements').html("no room on bus");
    }
    else if (distance > 15)
    {
        $('#announcements').html("no passenger nearby");
    }
}

這應該工作:

for (j = 0; j < shuttle.seats.length; j++) {

  // if a seat is empty
  if (shuttle.seats[j] == null && PASSENGERS[i].house != "Thayer Hall") {

    // remove picture from the 3-D map
    var features = earth.getFeatures();
    features.removeChild(PASSENGERS[i].placemark);

    // remove marker from the 2-D map
    PASSENGERS[i].marker.setMap(null);

    // remove 2-D map attribute of passenger
    PASSENGERS[i].marker = null;

    // add to the shuttle
    shuttle.seats[j] = PASSENGERS[i];

    // update the chart
    chart();

    $('#announcements').html("Passenger picked up!");
    $('#announcements').html("Score: " + score);

    num_pass++;

  // @lukpaw explanation: Code below was unnecessary
  //}
  console.log(shuttle.seats[j]);

  // @lukpaw explanation: Code below was unnecessary
  //}
  }
  else if (num_pass > 9) {
    $('#announcements').html("no room on bus");
  } else if (distance > 15) {
    $('#announcements').html("no passenger nearby");
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM