站内搜索: 请输入搜索关键词

当前页面: JAVA 编程资料牛鼻论坛Java & J2SE 技术区→求一个java script里的计算问题

求一个java script里的计算问题

发表新主题   回复此主题

第1楼 2007-09-18 19:48 茄子 写道:

求一个java script里的计算问题

一个input控件,里面录入的数据例如:93-139-1-60,
中间的“-”可能为其他的字符,如“/”,不用考虑,只计算其中的数字。
算法:=(|93-139|)+(|139-1|+1)+(|1-60|+1)=47+139+60=245,两个两个相减取绝对值,如果有1的话,就加1,然后再加起来,请教用java script该如何实现?

第2楼 2013-08-31 12:44 Robot :

求一个java script里的计算问题 相关


第3楼 2007-09-19 17:54 菜鹰 写道:

http://bbs.blueidea.com/thread-2792255-1-1.html
使用正则表达式分解数据
下面的不使用正则表达式:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>test</title>
</head>
<body>
<script type="text/javascript">
var str="-";//分隔符号
v_t=0;
function c(){
var obj=document.getElementById('a');
var obj_array=obj.value.split(str);
for (var i=1;i<obj_array.length;i++){
v_t+=Math.abs(obj_array[i-1]-obj_array[i])
if (obj_array[i-1]==1 ||obj_array[i]==1)v_t+=1
}
obj.value+=" 计算结果:"+v_t
}
</script>
<input id="a" type="text" value="请输入:" onfocus="this.value=''" style="width:200px">
<input type="button" value="OK" onclick="c()">
</body>
</html>

发表新主题   回复此主题