Javascrpit 的问题,高手们都看过来

王朝知道·作者佚名  2011-11-18
窄屏简体版  字體: |||超大  
 
分類: 電腦/網絡 >> 程序設計 >> 其他編程語言
 
問題描述:

我这里有一段计算器的代码,但是只能计算两个数,请帮我修改一下使它能计算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

小贴士:① 若网友所发内容与教科书相悖,请以教科书为准;② 若网友所发内容与科学常识、官方权威机构相悖,请以后者为准;③ 若网友所发内容不正确或者违背公序良俗,右下举报/纠错。
 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航