送分啦~~2个简单的Java题

2024-12-27 19:40:38
推荐回答(5个)
回答1:

第1题

import java.io.*;

public class TestLeapYear {

public static boolean isLeapYear(int year) {
return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
}

public static void main(String[] args) throws IOException {
BufferedReader br = null;
try {
System.out.print("输入年份:");
String strRead = "";
br = new BufferedReader(new InputStreamReader(System.in));
strRead = br.readLine();
int year = Integer.parseInt(strRead);
if (isLeapYear(year)) {
System.out.println(year + "年是闰年。");
} else {
System.out.println(year + "年是平年。");
}
} catch (Exception e) {
System.out.println("您输入的年份不是数字或不是四位数。提示:" + e.getMessage());
} finally {
br.close();
}
}
}

第二题

import java.io.*;

public class TestYearMonth {

public static boolean isLeapYear(int year) {
return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
}

public static void main(String[] args) throws IOException {
BufferedReader br = null;
String strYear = "";
String strMonth = "";
try {
System.out.print("输入年份:");
br = new BufferedReader(new InputStreamReader(System.in));
strYear = br.readLine();
System.out.print("输入月份:");
br = new BufferedReader(new InputStreamReader(System.in));
strMonth = br.readLine();
int year = Integer.parseInt(strYear);
int month = Integer.parseInt(strMonth);
if (month < 1 || month > 12) {
System.out.println("输入的月份不正确");
return;
}
if (month == 1 || month == 3 || month == 5 || month == 7
|| month == 8 || month == 10 || month == 12) {
System.out.println(year + "年" + month + "月的天数是31天");
} else if (month == 2 && isLeapYear(year)) {
System.out.println(year + "年" + month + "月的天数是29天");
} else if (month == 2 && !isLeapYear(year)) {
System.out.println(year + "年" + month + "月的天数是28天");
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
System.out.println(year + "年" + month + "月的天数是30天");
} else {
System.out.println("输入的月份不正确");
}

} catch (Exception e) {
System.out.println("您输入的年份不是数字或不是四位数。提示:" + e.getMessage());
} finally {
br.close();
}
}
}

回答2:

最简单的练习题啊……

回答3:

大学生?上来找作业题?

回答4:

有分也不能随便送啊,帮忙也不能随便帮啊。
这么简单的问题也问,怎么就能提高了……

回答5:

这么简单的题自己做吧。。。