我尝试了parseInt和parseFloat。 tot [0]总是返回一个数字。我通过使用console.log命令确保此时代码出错。 tot [1-9]返回NaN。我尝试使用while循环和for循环,结果相同。有人有答案吗?
var i = 0;
var base = [];
var bch = [];
var exp = [];
var charge = [];
var tot = [];
while(i<10)
{
base[i] = parseFloat(Math.floor(Math.random()*1000), 10);
exp[i] = parseFloat(Math.floor(Math.random()*9), 10);
charge[i] = parseFloat(Math.floor(Math.random()*10), 10);
if(charge[i] > 5)
{
charge[i] = -1;
exp[i] = exp[i]*charge[i];
}
else
{
charge[i] = 1;
exp[i] = parseFloat(exp[i]*charge[i], 10);
}
tot[i] = parseFloat(base[i]*Math.pow(10, exp));
console.log(tot[i]);
tot[i] = Math.ceil(tot[i]*10000000)/10000000;
bch[i] = parseFloat(Math.floor(Math.random()*10), 10);
if(bch[i] > 5)
{
bch[i] = -1;
tot[i] = tot[i]*bch[i];
}
else
{
bch[i] = 1;
tot[i] = tot[i]*bch[i];
}
$('#randnum'+ i).text(tot[i] + " " + base[i] + " " + exp[i]);
i = i+1;
}
HTML
<div id="contexp">
<div id="randnum0"></div>
<input id="abase0" name="" type="text" size="1px";>x10^
<input id="aexp0" name="" type="text" size="1px";>
<div id="check0"></div>
<br>
<div id="randnum1"></div>
<input id="abase1" name="" type="text" size="1px";>x10^
<input id="aexp1" name="" type="text" size="1px";>
<div id="check0"></div>
<br>
<div id="randnum2"></div>
<input id="abase2" name="" type="text" size="1px";>x10^
<input id="aexp2" name="" type="text" size="1px";>
<div id="check0"></div>
<br>
<div id="randnum3"></div>
<input id="abase3" name="" type="text" size="1px";>x10^
<input id="aexp3" name="" type="text" size="1px";>
<div id="check0"></div>
<br>
<div id="randnum4"></div>
<input id="abase4" name="" type="text" size="1px";>x10^
<input id="aexp4" name="" type="text" size="1px";>
<div id="check0"></div>
<br>
<div id="randnum5"></div>
<input id="abase5" name="" type="text" size="1px";>x10^
<input id="aexp5" name="" type="text" size="1px";>
<div id="check0"></div>
<br>
<div id="randnum6"></div>
<input id="abase6" name="" type="text" size="1px";>x10^
<input id="aexp6" name="" type="text" size="1px";>
<div id="check0"></div>
<br>
<div id="randnum7"></div>
<input id="abase7" name="" type="text" size="1px";>x10^
<input id="aexp7" name="" type="text" size="1px";>
<div id="check0"></div>
<br>
<div id="randnum8"></div>
<input id="abase8" name="" type="text" size="1px";>x10^
<input id="aexp8" name="" type="text" size="1px";>
<div id="check0"></div>
<br>
<div id="randnum9"></div>
<input id="abase9" name="" type="text" size="1px";>x10^
<input id="aexp9" name="" type="text" size="1px";>
<div id="check0"></div>
<br>
<input id="check2" type="button" value="check2">
</div>
<div id="totout"></div>
</div>
</body>