JavaScript从用户输入中查找平均值

时间:2014-11-19 03:05:52

标签: javascript average

我在计算mpgTotal方面遇到了麻烦。这应该将所有的mpgTankful加在一起,并将它们除以多少加在一起(基本上找到mpgTankful的平均值)。我尝试过实施一个柜台,但它并没有证明我喜欢它。它应该继续计数,直到用户输入-1退出。

示例结果将是:

Miles用户输入:
Gallons用户输入:
mpgTankful:
mpgTotal :(在这种情况下,mpgTankful将除以1)

Miles用户输入:
Gallons用户输入:
mpgTankful:
mpgTotal :(在这种情况下,用当前的mpgTankful添加以前的mpgTankful并除以2)

Miles用户输入:
Gallons用户输入:
mpgTankful:
mpgTotal :(在这种情况下,添加第一个mpgTankful,第二个和当前(第三个)mpgTankful并除以3)

var miles,      //Miles driven
    gallons,    //Gallons used
    mpgTankful, //MPG this tankful
    mpgTotal,   //Total MPG 
    mi,         //Miles driven converted to integer
    gal,        //Gallons used converted to integer
    count=0,
    avg;

//Enter -1 to quit
while(miles!="-1"){

//Read in the Miles Driven from user as a String
miles=window.prompt("Enter miles (-1 to quit):");
    if(miles=="-1"){
        break;
    }

//Read in the Gallons used from user as a String
gallons=window.prompt("Enter gallons:");

//Convert numbers from Strings to Integers
mi=parseInt(miles);
gal=parseInt(gallons);

//Calculate the MPG Tankful
mpgTankful=mi/gal;

//Calculate the Total MPG
mpgTotal+=mpgTankful;
count++;

if(count!=0){
    avg=mpgTotal/count;
}
else
    avg=0;

document.writeln("avg: " + avg);

//Print Results
document.writeln("<br/><br/>Miles driven: " + mi +
                 "<br/>Gallons used: " + gal +
                 "<br/>MPG this tankful: " + mpgTankful +
                 "<br/>Total MPG: " + mpgTotal);

}

1 个答案:

答案 0 :(得分:0)

你差不多......

mpgTotal+=mpgTankful;无法正常工作:第一次调用mpgTotal === undefined

if(count!=0)没用..你增加计数一行......

您计算了avg但未输出(根据&#39; Total MPG:&#39;的说明):<br/>Total MPG: " + mpgTotal

对于这个例子,我添加了一个无效输入的循环检查(注释中的解释)。

&#13;
&#13;
<script>
var    miles=0  //Miles driven
,    gallons=0  //Gallons used
, mpgTankful=0  //MPG this tankful
,   mpgTotal=0  //Total MPG 
,         mi=0  //Miles driven converted to integer
,        gal=0  //Gallons used converted to integer
,      count=0
,        avg=0
; // end vars

main: while(true){  //main loop, labeled 'main'
  if( (miles=prompt('Enter miles ("-1" to quit):')) === '-1' )
    break main;     //well.. plain english..
  if( isNaN(mi=parseInt(miles, 10)) ){  //check if we get a number
    alert('Please enter a number or enter "-1" to quit.');
    continue main;
  }
  while(true){
    if( (gallons=prompt('Enter gallons ("-1" to quit):')) === '-1' )
      break main; //NOT this inner loop.
    if( isNaN(gal=parseInt(gallons, 10)) ){  //check if we got a number
      alert('Please enter a number or enter "-1" to quit.');
      //no need to instruct a continue for this loop..
    } else break; //this inner loop (so we can go on with 'main' below).
  }
  
  mpgTankful=mi/gal;     //Calculate the MPG Tankful
  
  mpgTotal+=mpgTankful;  //Calculate the Total MPG
  
  avg=mpgTotal/++count;  //Calculate avg (and pre-increment count)
  
  //Print Results
  document.writeln( '<br/><br/>Miles driven: ' + mi
                  + '<br/>Gallons used: '      + gal
                  + '<br/>MPG this tankful: '  + mpgTankful
                  + '<br/>Total MPG: '         + avg
                  );
}
document.close(); //don't forget to close the body..
</script>
&#13;
&#13;
&#13;

这应该让你开始。