Javascrpit 的问题,高手们都看过来
我这里有一段计算器的代码,但是只能计算两个数,请帮我修改一下使它能计算8个数字,急用,谢了!好的话我会追加分数!
<html>
<head>
<title>Vance</title>
<script type = "text/javascript">
<!--
var a=0, // used to hold and pass temporary values.
secondNumber, // second string entered by user
number1, // first number to add
number2, // second number to add
sum; // sum of number1 and number2
// put button pressed into the text box
function show(a){
var thingy; // for holding value of text box, temporarily
thingy = document.calc.inout_box.value;
thingy = thingy + a;
document.calc.inout_box.value = thingy;
a=0;
}
// reset the formula to nothing
function restart(){
document.calc.inout_box.value = "";
}
// calculate and show the answer
function getanswer(){
var thingy; // for holding value of text box, temporarily
var tmp1; // for holding value of parse
var tmp2; // holds value of second number?
var positionInString; // for holding our position in the string
thingy = document.calc.inout_box.value;
//
tmp1 = parseFloat(thingy); // convert string to value which means a number
// alert is only used to show me what is happening - delete alert in final
alert("length of string is " + thingy.length);
alert(" First number is " + tmp1);
positionInString = thingy.indexOf("+");
tmp2 = parseFloat(thingy.substr(positionInString + 1, thingy.length));
// alert is only used to show me what is happening - delete alert in final
alert("Second number is " + tmp2);
alert("Length of second number is " + tmp2.toString().length);
tmp1 = tmp1 + tmp2;
//
restart();
show(tmp1);
}
// -->
</script>
参考答案:tmp1 = tmp1 + tmp2; 你这句不是只写计算2个数字吗?
你多写几个多定义你个不就好了 比如:
var tmp1,tmp2,tmp3,tmp4,tmp5,tmp6;
tmp1 = tmp1 + tmp2 + tmp3 + tmp4 + tmp5 + tmp6;
不行的话 加 QQ:3-1-2-6-2-7-4-5-9