这个判断瑞年平年的JAVA程序哪里有问题?看不出来

起个名字太难了202011 2013-07-17 11:00:47
import java.util.*;
public class in{
public static void main(String[] args){
System.out.println("请输入一个年份:");
Scanner input=new Scanner(System.in);
int year=input.nextInt();
/*
判断是否是瑞年还是平年(瑞年能被4整除并且不能被100整除||能
被400整除的年份是瑞年)
*/
if(year%4==0){
if(year%100!=0){
System.out.println(year+"这年是瑞年");
}
}
if(year%400==0){
System.out.println(year+"这年是瑞年");
}
else{
System.out.println(year+"这年是平年");
}
}
}
...全文
332 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
花木兰1闪21A 2013-07-17
  • 打赏
  • 举报
回复
if(year%100!=0)改为if(year%100==0)
  • 打赏
  • 举报
回复
EN,谢谢各位大神
a597926661 2013-07-17
  • 打赏
  • 举报
回复
楼主的第二个if应该放在第一个if的else里边

if( (year%4 == 0 && year%400 !=0) || (year%400 == 0) ){
   System.out.print("闰年");
}
else{
System.out.print("非闰年");
}
  • 打赏
  • 举报
回复
懂了。。。原来是else跟的是第二个if一起的,没跟第一个搞在一起
kitakyushu 2013-07-17
  • 打赏
  • 举报
回复
LZ代码执行了2个if-else 第一个为闰年 true 第二个为平年 false 故有2个输出 根据判断逻辑 true||false=true 得出404是闰年 LZ if-else的条件写的比较冗余
失落夏天 2013-07-17
  • 打赏
  • 举报
回复
你们测试下,我改了下,暂时没发现错误

public static void main(String[] args) {
		System.out.println("请输入一个年份:");
		Scanner input = new Scanner(System.in);
		int year = input.nextInt();
		/*
		 * 判断是否是瑞年还是平年(瑞年能被4整除并且不能被100整除||能 被400整除的年份是瑞年)
		 */
		if (year % 4 == 0) {
			if (year % 100 != 0&&year % 400 != 0) {
				System.out.println(year + "这年是瑞年");
				
			}else{
				if (year % 400 == 0) {
					System.out.println(year + "这年是瑞年");
				} else {
					//错误就在这里,不能被400整除的书并不一定不是闰年,比如404不能被400整除,但一样是闰年
					System.out.println(year + "这年是平年");
				}
			}
			
		}
		
	}
失落夏天 2013-07-17
  • 打赏
  • 举报
回复
首先注意错别字。 闰年 第二,你的错误:

public static void main(String[] args) {
		System.out.println("请输入一个年份:");
		Scanner input = new Scanner(System.in);
		int year = input.nextInt();
		/*
		 * 判断是否是瑞年还是平年(瑞年能被4整除并且不能被100整除||能 被400整除的年份是瑞年)
		 */
		if (year % 4 == 0) {
			if (year % 100 != 0) {
				System.out.println(year + "这年是瑞年");
			}
		}
		if (year % 400 == 0) {
			System.out.println(year + "这年是瑞年");
		} else {
			//错误就在这里,不能被400整除的书并不一定不是闰年,比如404不能被400整除,但一样是闰年
			System.out.println(year + "这年是平年");
		}
	}
  • 打赏
  • 举报
回复
没搞懂,讲明白点,我觉得没错啊(实际上没脑子没想明白)
x4652661 2013-07-17
  • 打赏
  • 举报
回复
条件没有分清楚,逻辑问题
  • 打赏
  • 举报
回复
第二个if(year%400==0)用404时不是不成立吗?应该不会输出来啊。。。
attach_finance 2013-07-17
  • 打赏
  • 举报
回复
import java.util.*;

public class in {
	public static void main(String[] args) {
		System.out.println("请输入一个年份:");
		Scanner input = new Scanner(System.in);
		int year = input.nextInt();
		/*
		 * 判断是否是瑞年还是平年(瑞年能被4整除并且不能被100整除||能 被400整除的年份是瑞年)
		 */
		if (year % 4 == 0) {
			if (year % 100 != 0||year % 400 == 0) {
				System.out.println(year + "这年是瑞年");
			}else {
				System.out.println(year + "这年是平年");
			}
		} else {
			System.out.println(year + "这年是平年");
		}
	}
}
闰年怎么算的,搞忘了,不知道是不是这么算的。。
kitakyushu 2013-07-17
  • 打赏
  • 举报
回复
两个判断条件 放在一个if里|| 就行了
attach_finance 2013-07-17
  • 打赏
  • 举报
回复
因为你第二个if(year%400==0)的原因
Zemo 2013-07-17
  • 打赏
  • 举报
回复
其实很简单. 把你的if(year%400==0)改成else if(year%400==0)就行了. 不满足上面两个条件才是平年.


import java.util.*;


public class in {
	public static void main(String[] args) {
		System.out.println("请输入一个年份:");
		Scanner input = new Scanner(System.in);
		int year = input.nextInt();
		/*
		 * 判断是否是瑞年还是平年(瑞年能被4整除并且不能被100整除||能 被400整除的年份是瑞年)
		 */
		if (year % 4 == 0) {
			if (year % 100 != 0) {
				System.out.println(year + "这年是瑞年");
			}
		}else if (year % 400 == 0) {
			System.out.println(year + "这年是瑞年");
		} else {
			System.out.println(year + "这年是平年");
		}
	}
}
邹邹wl 2013-07-17
  • 打赏
  • 举报
回复
你这代码后面的个if else与前面的if无关,so第一个if能进去,后面的else也能走

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧