Javascript数组,将摄氏温度转换为华氏温度

时间:2016-11-19 23:31:35

标签: javascript

我需要将6摄氏度的温度转换为华氏温度。我还需要计算平均温度。我必须在我的解决方案中使用2个数组。我想我在这里找到答案非常接近。我是JavaScript的新手。

var numIn1 = parseInt(prompt("Please enter temperature for January in    Celsius"));
var numIn2 = parseInt(prompt("Please enter temperature for February in Celsius"));
var numIn3 = parseInt(prompt("Please enter temperature for March in Celsius"));
var numIn4 = parseInt(prompt("Please enter temperature for April in Celsius"));
var numIn5 = parseInt(prompt("Please enter temperature for May in Celsius"));
var numIn6 = parseInt(prompt("Please enter temperature for June in Celsius"));
var degree = new Array(numIn1, numIn2, numIn3, numIn4, numIn5, numIn6);
var Average = 0;
var Total = 0;
for(i = 0; i < degree.length; i++) {
  Total = Total + degree[i];
}

function convert(degree) {
  var i;
  if(degree == "C") {
    i = document.getElementById("c").value * 9 / 5 + 32;
    document.getElementById("f").value = Math.round(x);
  } else {
    i = (document.getElementById("f").value - 32) * 5 / 9;
    document.getElementById("c").value = Math.round(x);
  }
}
Average = Total / degree.length;
alert(degree[0]);
alert(degree[1]);
alert(degree[2]);
alert(degree[3]);
alert(degree[4]);
alert(degree[5]);
alert(Average);
alert(Math.round(Average));

1 个答案:

答案 0 :(得分:0)

这是一个有效的解决方案。希望它有所帮助!

&#13;
&#13;
var arr = new Array(6);
     var sum = 0;
     var Average = 0;

     for(var i = 0 ; i < 6; i++){
        var month = '';
        if(i==0) month = "January"; 
        if(i==1) month = "February";
        if(i==2) month = "March";
        if(i==3) month = "April";
        if(i==4) month = "May";
        if(i==5) month = "June";
        arr[i] = parseInt(prompt("Please enter temperature for " + month + " in Celsius"));

        sum += arr[i];
        Average = sum/arr.length;
     }

     for(var j = 0 ; j < arr.length; j++){
        alert("Convertion of: " +arr[j]+" to Fahrenheit is: "+ convertToF(arr[j]));
     }


     alert("The Average is: "+ Math.round(Average));
     alert("The Sum is: "+ sum);


     function convertToF(cTempVal) {
      var fTempVal = (cTempVal * (9 / 5)) + 32;
       return fTempVal;
        
    }
&#13;
&#13;
&#13;