闰年确定 [英] Leap Year dertermination

查看:80
本文介绍了闰年确定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以协助我的代码。我只是不能让它工作:

我必须写一个脚本,允许用户输入一年和一年然后确定它是否是闰年。该请求适用于带有单个文本框的表单,并使用警告对话框说明输入的年份是标准年份还是闰年。


代码到目前为止:

[HTML]< HTML>

< HEAD>

< TITLE>这是闰年吗?< / TITLE>

< / HEAD>


< SCRIPT LANGUAGE =" JAVASCRIPT>

<! - 隐藏旧浏览器


函数leapYear(){

Last2Digits =年%100

if(Last2Digits == 0):

flag = year%400

else:

flag = year%4


if(flag == 0):

window.alert(你输入的那一年是闰年。)

else:

window.alert(&你输入的年份是标准年份。)

}


// - >

< /脚本>

< / head>

< body>

< form>< p>年份:< inp ut type =" text" name =" Year">< / p>

< input type =" button" value ="检查闰年 name =" check for leap year" onclick =" leapYear()">

< / form>

< / body> [/ HTML]

解决方案

您可以使用Date对象的一个​​技巧。首先设定日期为2月29日,YYYY。然后从同一Date对象获取月份。如果你得到2月它是闰年,如果你得到March它不是。


注意,数月从0到11编号。

没有,你在那里失去了我......其他任何人都有一些简单的修复方法。这个问题只值15分,但随着我花在这上面的时间,它已经是150 !!!

展开 | 选择 | Wrap | 行号

以下工作方式:


[HTML]< title>这是闰年吗?< / title>

< head>

< script>

function leapYear(year){

var Last2Digits = year%100;

if(Last2Digits == 0){

flag = year%400;

} else {

flag = year%4;

}


if(flag == 0){

window.alert(" The您输入的年份是闰年。);

}其他{

window.alert(您输入的年份是标准年份。 );

}

}

< / script>

< / head>

< body>

< form>

< p>年份:

< input type =" text" ; ID = QUOT;年"命名= QUOT;年份">

< / p>

< input type =" button" value ="检查闰年名称= QUOT; check_for_leap_year" onclick =" leapYear(document.getElementById(''year'')。value);">

< / form>

< / body>

[/ HTML]


请将它与你的工作代码进行比较...看看你的head-tags,更正你的if语句将这一年传递给你的函数,直到它预计一年可以使用...


你所犯的错误非常简单......我建议你仔细检查语法和使用的值和变量...试试!!!使用firefox和firebug-extension,可以立即显示测试结果,你的代码有什么问题......


亲切的问候...


Can anyone assist with my code. I just cannot get this to work:
I have to write a script that allows the user to enter a year & then determine whether it is a leap year or not. The request is for a form with a single text box and using an alert dialog box stating if the entered year is a standard or leap year.

Code so far:
[HTML]<HTML>
<HEAD>
<TITLE>Is it a leap year?</TITLE>
</HEAD>

<SCRIPT LANGUAGE="JAVASCRIPT>
<!-- Hide from old browsers

function leapYear(){
Last2Digits = year % 100
if (Last2Digits == 0):
flag = year % 400
else:
flag = year % 4

if (flag == 0) :
window.alert("The year you have entered is a leap year.")
else:
window.alert("The year you have entered is a standard year.")
}

//-->
</script>
</head>
<body>
<form><p>Year: <input type="text" name="Year"></p>
<input type="button" value="Check for Leap Year" name="check for leap year" onclick="leapYear()">
</form>
</body>[/HTML]

解决方案

There is one trick you could use with Date object. First set date to "Feb 29, YYYY" and then get month from the same Date object. If you get Feb it''s leap year, if you get March it''s not.

Beware, months are numbered from 0 - 11.


Nope, you''ve lost me there... anyone else got some ideas for a simple fix. The question is only worth 15 marks, but with the amount of time I have spent on this it shoud have been 150!!!

Expand|Select|Wrap|Line Numbers


the following works your way:

[HTML]<title>Is it a leap year?</title>
<head>
<script>
function leapYear(year) {
var Last2Digits = year % 100;
if (Last2Digits == 0) {
flag = year % 400;
} else {
flag = year % 4;
}

if (flag == 0) {
window.alert("The year you have entered is a leap year.");
} else {
window.alert("The year you have entered is a standard year.");
}
}
</script>
</head>
<body>
<form>
<p>Year:
<input type="text" id="year" name="Year">
</p>
<input type="button" value="Check for Leap Year" name="check_for_leap_year" onclick="leapYear(document.getElementById(''year''). value);">
</form>
</body>
[/HTML]

please compare it to your not-working code ... have a look at your head-tags, correct your if-statements, pass the year to your function until it expects year to work with ...

the errors you make are really simple ... and i would recommend that you double-check the syntax and the used values and variables ... try!!! to use firefox and the firebug-extension that shows you instantly on testing, whats wrong with your code ...

kind regards ...


这篇关于闰年确定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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