编写一个Java程序,对于给定的年份,回答“Leap Year”(闰年)或者“Not a Leap Year”(平年)。如果一个年份能被4整除,但是不能被100整除,它是闰年;如果一个年份能被100整除,也能被400整除,它也是闰年。需要定义名为LeapYear的服务提供类

题目
编写一个Java程序,对于给定的年份,回答“Leap Year”(闰年)或者“Not a Leap Year”(平年)。如果一个年份能被4整除,但是不能被100整除,它是闰年;如果一个年份能被100整除,也能被400整除,它也是闰年。需要定义名为LeapYear的服务提供类
参考答案和解析

如果没有搜索结果或未解决您的问题,请直接 联系老师 获取答案。
相似问题和答案

第1题:

下列程序要求用户输入一个年份,并能够判断用户输入的年份是否是闰年,横线处应填 Sub EnSure() Dim year As Integer Year=Val(InputBox("请输入年份:")) If(year Mod 4=0_____yaer Mod 100<>0)______(yaer Mod 400=0)Then Print"您输入的是闰年" Else Print"不是闰年,是普通年份" End If End SubA.And And B.And Or C.Or Or D.Or Or


正确答案:B
【解析】闰年的判断条件是:年份是4的倍数并且不是100的倍数,或者年份是400的倍数。所以表达式为:(year Mod 4=0 And year Mod 100<>0) Or (year Mod 400=0)。

第2题:

给定年份,下列程序用来判断该年是否为闰年,请填空。

提示:闰年的条件是年份可以被4整除但不能被100整除,或者能被400整除。

Private Sub Comand6_Click()

Dim y As Integer

y=InputBox("请输入年份")

If(y Mod 4=0______y Mod 100<>0)or(y Mod 400=0)Then

Print"是闰年"

Else

Print"是普通年份"

End If

End Sub


正确答案:And
And

第3题:

请在每条横线处填写一个语句,使程序的功能为:判断输入的年份是否为闰年(例如:1998年不是闰年,2000年是闰年).

注意:请勿改动main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。

import java.io.*;

public class LeapYear{

public static void main(String args[]){

___________________;

BufferedReader in;

ir=new InputStreamReader(_____________________________);

in=new BufferedReader(ir);

System. out. print In("输入年份是: ");

String s=in.readline();

int year=___________________

if(year%4==0&&year%100!=0||year%400==0

System.out.println(" "+year+" "年是闰年. ");

else

System.out.println(" " +year+ " "年不是闰年.");

}

}

}


正确答案:InputStreamReader ir; System.in Integer.parseInt(S);
InputStreamReader ir; System.in Integer.parseInt(S); 解析:本题主要考查I/O流。解答本题的关键是理解和使用I/O流。在本题中,InputStreamReader ir;语句的功能是声明一个字符输入流对象ir,ir=new InputStreamReader(System.in);语句的功能是生成一个字符输入流对象ir(InputStreamReader();方法以标准输入流对象为参数)。int year=Integer,parseInt(s);语句的功能是定义一个整型变量year,这里Integer是包装类,parseInt(s)方法是将字符串数据转换成整型数据。

第4题:

输出1900~2000年中所有的闰年。每输出3个年号换一行。(判断闰年的条件为下面二者之一:能被4整除,但不能被100整除。或者能被400整除。)


正确答案:
#include”stdio.h”
main
{intI,n;
for(n=0,I=1900;I<=2000;I++)
{if(I%4==0I0!=0||I@0==0)
{printf(“%d ”,I); n++; }
if(n%3==0)
printf(“\n”); } } }

第5题:

下面程序是判断某一个是否为闰年,请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。(闰年的条件是符合下面两者之一:①能被4整除,但不能被100整除;②能被4整除,又能被100整除)。

注意:不改动程序的结构,不得增行或删行。

import java.io.*;

public class LeapYear

{

public static void main(String args[])

{

int year=1979;

if((year %4= =0 || year % 100 !=0) || (year % 400= =0))

System.out.println(year+"是闰年.");

else

System.out.println(year+"不是闰年。");

year=2000;

boolean leap;

if(year % 4 !=0)

leap=false;

else if(year % 100 !=0)

leap=true;

else if(year % 400 !=0)

leap=false;

else

leap=true;

if(______)

System.out.println(year+"是闰年。");

else

System.out.println(year+"不是闰年。");

year=2010;

if(year % 4= =0)

{

if(year % 100= =0)

{

if(year % 400= =0)

leap=true;

else

______

}

else

leap=false;

}

if(1eap= =true);

System.out.println(year+"是闰年。");

else

System.out.println(year+"不是闰年。");

}

}


正确答案:(year%4= =0&&year%100!=0)||(year%400 ==0) Year%4==0
(year%4= =0&&year%100!=0)||(year%400 ==0) Year%4==0 解析:本题综合考查Java语言的数据类型及运算和分支语句。第1处的错误是:(year%4==0||year%100!=0)||(year%400==0);闰年的条件是符合下面二者之一:①能被4整除,但不能被100整除;②能被4整除,又能被100整除。因此判断某一年是否为闰年的表达式应该为(year%4==0&&year%100!=0)||(year%400==0)。第2处的错误是year%4!=0;应该填写year%4==0,用来判断该年是否能被4整除。

第6题:

请补充fun函数,该函数的功能是:判断一个年份是否为闰年。

例如,1900年不是闰年,2004是闰年。

[注意] 部分源程序给出如下。

请勿改动主函数main和其他函数中的任何内容,仅在函数fun的横线上填入所编写的若干表达式或语句。

[试题源程序]

include<stdio.h>

include<conio.h>

int fun(int n)

{

int fiag=0;

if(n%4==0)

{

if( (1) )

fiag=1;

}

if( (2) )flag=1;

return (3) ;

}

void main()

{

int year;

clrscr();

printf("Input the year:");

scanf("%d", &year);

if(fun(year))

printf("%d is a leap year.\n", year);

else

printf("%d is not a leap year.\n", year);

}


正确答案:[1] n%100 !=0 [2] n%400==0 [3] flag
[1] n%100 !=0 [2] n%400==0 [3] flag 解析:题的关键是要知道闰年的定义,如果年份能被4整除但不能被100整除,或者能被400整除,这两种情况满足之一都是闰年。
填空1:显然此处属于第一种情况,能被4整除但不能被100整除。所以这里应该填n%100!=0,只有满足这个条件,flag才能被赋值1。
填空2:此处属于第二种情况,能被400整除,所以应该填n%400==0。
填空3:从程序中可以知道,当满足以上两种情况之一时,flag就被赋值1,显然flag属于标志单元。再看主函数中对fun()函数的调用,显然需要返回的值不是真就是假,即函数fun()中标志单元flag的内容。

第7题:

请完成下列Java程序:输出某年的某月有多少天数,要求生成2个输入对话框,一个输入年份,一个输入月份,并且能检查输入的数字是否符合要求,如果符合要求,则根据输入的年份和月份计算出这一年的这一月有多少天。提示:闰年为年数能被4整除但是不能被100整除。

注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。

程序运行结果如下:

import javax.swing. JOptionPane;

public class ex10_2 {

public static void main(String[] args) (

String strIn;

int nYear,nMonth,nNumOfDays=0;

strIn=JOptionPane.showInputDialog(,,year.);

nYear=Integer.parseInt(strIn);

if(nYear<0) {

System.out.println("Invalidate number of year!")

return;

} strIn=JOptionPane.showInputDialog("month.);

nMonth=Integer.parseInt(strIn);

if(_____________) {

System.out.println("Invalidate number of month!"

return;

}

switch(nMonth){

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

nNumOfDays=31;

break;

case 4:

case 6:

case 9:

case 11:

nNumOfDays=30;

break;

case 2:

if(________________)

nNumOfDays=29;

else

nNumOfDays=28;

break;

}

System.out.println("Number of days:"+ nNumOfDays);

}

}


正确答案:nMonth0 || nMonth>12 ((nYear%4 ==0)&&!(nYear%100 == 0)) || (nYear%400 == 0)
nMonth0 || nMonth>12 ((nYear%4 ==0)&&!(nYear%100 == 0)) || (nYear%400 == 0) 解析:本题主要考查swing图形用户界面设计和case语句的简单应用。解题关键是会用swing的容器面板构造对话框,熟悉判断闰年的条件语句的设计,判断输入是否符合程序要求的条件语句设计,case语句的设计。本题中,第1个空,判断输入的月份值应该在1到12月之间,否则给出提示信息并退出;第2个空,闰年的判断,将题干中的提示用代码实现。

第8题:

能被2整除的数叫做( ),不能被2整除的数叫做( )。


正确答案:
能被2整除的数叫做偶数,不能被2整除的数叫做奇数

第9题:

下列程序要求用户输入一个年份,并能够判断用户输入的年份是否是闰年,下划线处应填( )。 Sub EnSure() Dim year As Integer year=Val(InputBox("请输入年份:")) If(year Mod 4=0 ______ year Mod 100<>0) ______ (year Mod 400=0) Then Print "您输入的是闰年" Else Print "不是闰年,是普通年份" End If End Sub

A.And And

B.And Or

C.Or Or

D.Or Or


正确答案:B
解析:闰年的判断条件是:年份是4的倍数并且不是100的倍数,或者年份是400的倍数。所以表达式为:(yearMod4=0AndyearMod100>0)Or(yearMod400=0)。

第10题:

下列程序要求用户输入一个年份,并能够判断用户输入的年份是否是闰年,横线处应填( )。

Sub EnSure()

Dim year As Integer

year=Val(InputBox("请输入年份:"))

If(year Mod 4=0______year Mod 100<>0)______(year Mod 400=0)Then

Print"您输入的是闰年"

Else

Print"不是闰年,是普通年份"

End If

End Sub

A.And And

B.And Or

C.Or Or

D.Or Of


正确答案:B
解析:闰年的判断条件是:年份是4的倍数并且不是100的倍数,或者年份是400的倍数。所以表达式为:(year Mod 4=0 And year Mod 100>0) Or (year Mod 400=0)。

更多相关问题