java脚本中的阶乘数字 [英] factorial numbers in java script

查看:68
本文介绍了java脚本中的阶乘数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,我现在只是写一个基本的计算,然后在它上面建立一个A-Level工作的
。我可以添加/分割等...

两个数字在一起但我遇到了以下主要问题

计算:


z = x! /(x-y)!


以下代码是我的尝试,我希望在

正确方向上的一个点也是我要去的地方错误..欢呼


< html>


< head>


< FORM NAME = frmOne>


第一名:< INPUT TYPE =文字名称= txtFirstNumber SIZE = 5值

="">


第二个:< INPUT TYPE =文本名称= txtSecondNumber SIZE = 5值

="">

< ; P>

总计:< INPUT TYPE =文字名称= txtThirdNumber SIZE = 5 value ="">

< P>

<输入类型=按钮名称= b1 VALUE ="计算" onClick =

计算()>

< / FORM>


< script language =" javascript"> ;

函数calculate(){

A = document.frmOne.txtFirstNumber.value

B = document.frmOne.txtSecondNumber.value

C =(A!/(A - B)!)

document.frmOne.txtThirdNumber.value = C

}


< / script>


< / head>


< body>刷新以再次运行脚本


< / body>


Patrick

解决方案

Patrick,


javascript中没有factorial运算符。 A!不是有效的java

表达式。如果你想做阶乘,你需要自己编写

函数,或者搜索一个例子,其中我确定

成千上万。


john


啊抱歉...我在哪里调用函数看到我

已经有''函数计算''?我应该废弃所有这些吗?

从阶乘功能开始?


patrick


< blockquote> 2005年12月9日18:37,patrick_woflian写道:

[...]我遇到了以下计算中的一个主要问题:
z = x! /(x-y)!


虽然有一元人! ECMAScript中的运算符,它不是b
算术。事实上,它是一个逻辑NOT运算符,就像许多其他

编程语言一样。


没有内置的计算阶乘的方法,所以你需要

自己编写(这不应该是困难的)。


[snip]

< head>

< FORM NAME = frmOne>


您不能在文档的HEAD元素中放置FORM元素。

SCRIPT元素可能保留在HEAD中,但FORM必须移动到

BODY元素。


[snip]

<输入类型=按钮名称= b1 VALUE ="计算" onClick =
calculate()>


必须引用onclick属性值。除非你知道规则

什么可以,什么不能引用,做简单的事情并引用

所有属性值。


[snip]

< script language =" javascript">


语言属性早已被弃用。改为使用type属性




< script type =" text / javascript">

函数计算( ){
A = document.frmOne.txtFirstNumber.value
B = document.frmOne.txtSecondNumber.value
C =(A!/(A - B)!)




这些变量将变为全局变量,这不是一种好的做法。首先用var关键字声明




还可以进行其他改进,但是他们可以等待

现在。我建议的一件事是你统一标记文件

。混合的案件看起来有点混乱。全部小写(对于

元素和属性)通常更容易阅读。


Mike


-

Michael Winter

在通过电子邮件回复之前用[新闻]作为前缀主题。


hey guys, im just writing a basic calculation at the moment, before
building on it for an A-Level piece of work. i can add/divide etc...
two numbers together yet i am having a major problem with the following
calculation:

z = x! / (x- y)!

The following code is my attempt and i was hoping for a point in the
right direction as too where i am going wrong.. cheers

<html>

<head>

<FORM NAME = frmOne>

Number One: <INPUT TYPE = Text NAME = txtFirstNumber SIZE = 5 value
="">

Number Two: <INPUT TYPE = Text NAME = txtSecondNumber SIZE = 5 value
="">
<P>
Total: <INPUT TYPE = Text NAME = txtThirdNumber SIZE = 5 value = "">
<P>
<Input Type = Button NAME = b1 VALUE = "Calculate" onClick =
calculate()>
</FORM>

<script language = "javascript">
function calculate() {
A = document.frmOne.txtFirstNumber.value
B = document.frmOne.txtSecondNumber.value
C = (A ! / (A - B ) ! )
document.frmOne.txtThirdNumber.value = C
}

</script>

</head>

<body> Refresh to run the script again

</body>

Patrick

解决方案

Patrick,

There is no factorial operator in javascript. "A !" is not a valid java
expression. If you want to do factorials, you''ll need to either write
the function yourself, or search for an example, of which I''m sure
there are thousands.

john


ahhhh sorry about that.. where would i call the function seeing as i
already have the ''function calculate''? should i scrap all that and
start with the factorial function?

patrick


On 09/12/2005 18:37, patrick_woflian wrote:

[...] i am having a major problem with the following calculation:

z = x! / (x- y)!
Whilst there is a unary ! operator in ECMAScript, it is not
arithmetical. It is, in fact, a logical NOT operator, as in many other
programming languages.

There is no built-in method for calculating factorials, so you will need
to write your own (which shouldn''t be difficult).

[snip]
<head>

<FORM NAME = frmOne>
You may not place a FORM element within the HEAD element of a document.
The SCRIPT element may remain within HEAD, but the FORM must be moved to
the BODY element.

[snip]
<Input Type = Button NAME = b1 VALUE = "Calculate" onClick =
calculate()>
That onclick attribute value must be quoted. Unless you know the rules
for what can, and what cannot be quoted, do the simple thing and quote
all attribute values.

[snip]
<script language = "javascript">
The language attribute has long been deprecated. Use the type attribute
instead:

<script type="text/javascript">
function calculate() {
A = document.frmOne.txtFirstNumber.value
B = document.frmOne.txtSecondNumber.value
C = (A ! / (A - B ) ! )



These variables will become global, which is not good practice. Declare
them with the var keyword first.

There are other improvements that could be made, but they can wait for
now. One thing that I would recommend is that you mark-up documents
uniformly. Mixed case looks a bit of a mess. All lowercase (both for
elements and attributes) is generally easier to read.

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.


这篇关于java脚本中的阶乘数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆