javascript数组输入用户和平均计算

时间:2015-07-20 12:11:06

标签: javascript arrays

我有这个问题需要解决。 这听起来很简单,但我是一个初学者。 这是我的代码到目前为止,但是当我运行它时只出现一个窗口。 感谢您的建议和帮助

var years= prompt("Enter the number of years: "); // Number of years
var rainFall = new ArrayList();

while (years < 1) {

    alert("Invalid. Enter 1 or greater: ");
    years =prompt("Enter a year: ");
}

for (var y = 1; y <= years; y++) {

    for (var m = 1; m <= NUM_MONTHS; m++) {

        alert("Year " + y + " month " + m + ": ");
        monthRain = prompt("enter rainfall for a month ");
        rainfall.add(monthRain);
    }
}

 alert("\nNumber of months: " + (years * NUM_MONTHS) + "Total rainfall: " + calculateTotalRainFall(rainFall) + " inches" + "Average monthly rainfall: " + calculateAverageRainFall(rainFall) + " inches");

3 个答案:

答案 0 :(得分:1)

    Array.prototype.sum = function() {
      var total = 0;
      for(var i in this) {
        var i = parseFloat(this[i]);
        if(i>=0) {
          total += i;
        }
      }
      return total;
    };

    Array.prototype.avg = function() {
      var avg = (this.length>0)? parseFloat(this.sum()/this.length) : 0;
      avg = parseFloat(parseInt(avg*100)/100);
      return avg;
    };

    var years = parseInt(prompt("Enter the number of years: ")); // Number of years

    var rainFall = [];

    while (years < 1) {
      alert("Invalid. Enter 1 or greater: ");
      years = parseInt(prompt("Enter a year: "));
    }

    var NUM_MONTHS = 12;
    for (var y = 1; y <= years; y++) {
      for (var m = 1; m <= NUM_MONTHS; m++) {
        alert("Year " + y + " month " + m + ": ");

        var monthRain = parseFloat(prompt("enter rainfall for a month "));
        if(monthRain>=0) {
          rainFall.push(monthRain);
        }
      }
    }

 if(years<1) years = 0;
 alert("\nNumber of months: " + (years * NUM_MONTHS) + "   Total rainfall: " + rainFall.sum() + " inches" + "   Average monthly rainfall: " + rainFall.avg() + " inches");

    function sumArray (items) {
      var total = 0;
      for(var i in items) {
        var i = parseFloat(items[i]);
        if(i>=0) {
          total += i;
        }
      }
      return total;
    };

    function avgArray(items) {
      var sum = sumArray(items);
      var avg = (items.length>0)? parseFloat(sum/items.length) : 0;
      avg = parseFloat(parseInt(avg*100)/100);
      return avg;
    };

    var years = parseInt(prompt("Enter the number of years: ")); // Number of years

    var rainFall = [];

    while (years < 1) {
      alert("Invalid. Enter 1 or greater: ");
      years = parseInt(prompt("Enter a year: "));
    }

    var NUM_MONTHS = 12;
    for (var y = 1; y <= years; y++) {
      for (var m = 1; m <= NUM_MONTHS; m++) {
        alert("Year " + y + " month " + m + ": ");

        var monthRain = parseFloat(prompt("enter rainfall for a month "));
        if(monthRain>=0) {
          rainFall.push(monthRain);
        }
      }
    }

 if(years<1) years = 0;
 alert("\nNumber of months: " + (years * NUM_MONTHS) + "   Total rainfall: " + sumArray(rainFall) + " inches" + "   Average monthly rainfall: " + avgArray(rainFall) + " inches");

答案 1 :(得分:0)

尝试更改rainfall.add(monthRain); to rainfall.push(monthRain);

答案 2 :(得分:0)

当您应该使用 JavaScript Array 类时,您的核心错误似乎是使用 Java ArrayList 类。请记住,Java和JavaScript不相关(名称令人困惑,是的)。

如果您将new ArrayList()更改为new Array(),将rainfall.add更改为rainFall.push(请注意大写更改),那么它似乎可以正常工作。

如果您可以访问JavaScript控制台检查器(例如,可以使用ctrl-shift-J在Chrome中访问),则可以查找代码中出现的JS错误,例如{{1时遇到的错误无法找到。

修正如下。我还添加了缺少的NUM_MONTHS常量(我假设你在其他地方)。

ArrayList