我的代码似乎部分工作:我的意思是,当我没有进入闰年例如99结束然后我选择2月2日打印29天,我想打印28我有2个separete方法,一个检查年份是否跳跃,另一个打印日期,所以如果有人知道如何修复程序以提高效率,并且没有闰年显示28天,我将不胜感激。谢谢。
这是我的代码:
import java.util.Scanner;
public class LeapYearCheck
{
public static void main(String[] args)
{
LeapYearCheck.isLeapYear();
LeapYearCheck.daysInMonth();
}
static void isLeapYear()
{
Scanner input = new Scanner(System.in);
System.out.println("Enter a year: ");
int year = input.nextInt();
if(year % 4 == 0 || year % 400 ==0)
{
System.out.println(year + " is leap year:");
}
else
{
System.out.println(year + " is not leap year:");
}
}
static void daysInMonth()
{
Scanner input = new Scanner(System.in);
System.out.println("Enter a month :");
int month = input.nextInt();
if (month == 2)
{
System.out.println("There are 29 days in February: ");
}
else if(month == 1)
{
System.out.println("The are 31 days in January ");
}
else if(month == 2)
{
System.out.println("The are 28 days in February ");
}
else if(month == 3)
{
System.out.println("The are 31 days in March ");
}
else if(month == 4)
{
System.out.println("The are 30 days in April");
}
else if(month == 5)
{
System.out.println("The are 31 days in May ");
}
else if(month == 6)
{
System.out.println("The are 30 days in June ");
}
else if(month == 7)
{
System.out.println("The are 31 days in July ");
}
else if(month == 8)
{
System.out.println("The are 31 days in August ");
}
else if(month == 9)
{
System.out.println("The are 30 days in September ");
}
else if(month == 10)
{
System.out.println("The are 31 days in October ");
}
else if(month == 11)
{
System.out.println("The are 30 days in November ");
}
else if(month == 12)
{
System.out.println("The are 31 days in December ");
}
else
{
System.out.println("Invalid Month, Please enter a number between 1 & 12 Merci: ");
}
}
}
答案 0 :(得分:2)
您的闰年检查错误。你应该使用
if((year%100 != 0 && year%4 == 0) || year % 400 ==0))
答案 1 :(得分:2)
它将始终打印
二月有29天:
因为第一次检查如下:
if (month == 2)
{
System.out.println("There are 29 days in February: ");
}
另外,检查闰年的正确方法:
if ((year % 400 == 0) || (year % 100 != 0 && year % 4 == 0)) {
//Leap year.
}
正确的代码:
if (month == 2) {
if (LeapYearCheck.isLeapYear()) {
System.out.println("There are 29 days in this month.");
} else {
System.out.println("There are 28 days in this month.");
}
}
让isLeapYear()
函数返回boolean
(而不是void
)。
答案 2 :(得分:0)
除了hovanessyan的suggestion之外,考虑到有关年份是否为闰年的信息会在isLeapYear
函数的范围内保留和丢失。例如,此函数应返回一个布尔值,并在daysInMonth
内调用,因此daysInMonth
可以相应地返回28或29。使用Map
也可以帮助简化巨大的if-else块。
答案 3 :(得分:0)
简单地说:daysInMonth()
方法需要知道它是哪一年,然后如果月份数是2,则需要检查它是否是闰年。修改您的方法以接受年份和/或月份作为参数而不是提示它们,然后在main()
中提示年份和月份,并将必要的数据传递给每个方法。
答案 4 :(得分:0)
首先,如果月号为2,则输入29天。
if (month == 2)
{
System.out.println("There are 29 days in February: ");
}
答案 5 :(得分:0)
尝试此代码....
import java.util.Scanner;
public class LeapYearCheck {
static boolean isleep;
public static void main(String[] args) {
LeapYearCheck.isLeapYear(); LeapYearCheck.daysInMonth();
}
static void isLeapYear() {
Scanner input = new Scanner(System.in);
System.out.println("Enter a year: ");
int year = input.nextInt();
if(year % 4 == 0 || year % 400 ==0)
{
isleep=true;
System.out.println(year + " is leap year:");
}
else
{
isleep=false;
System.out.println(year + " is not leap year:");
}
}
static void daysInMonth()
{
Scanner input = new Scanner(System.in);
System.out.println("Enter a month :");
int month = input.nextInt();
if (month == 2)
{
if(isleep)
System.out.println("There are 29 days in February: ");
else
System.out.println("There are 28 days in February: ");
}
else if(month == 1)
{
System.out.println("The are 31 days in January ");
}
else if(month == 3)
{
System.out.println("The are 31 days in March ");
}
else if(month == 4)
{
System.out.println("The are 30 days in April");
}
else if(month == 5)
{
System.out.println("The are 31 days in May ");
}
else if(month == 6)
{
System.out.println("The are 30 days in June ");
}
else if(month == 7)
{
System.out.println("The are 31 days in July ");
}
else if(month == 8)
{
System.out.println("The are 31 days in August ");
}
else if(month == 9)
{
System.out.println("The are 30 days in September ");
}
else if(month == 10)
{
System.out.println("The are 31 days in October ");
}
else if(month == 11)
{
System.out.println("The are 30 days in November ");
}
else if(month == 12)
{
System.out.println("The are 31 days in December ");
}
else
{
System.out.println("Invalid Month, Please enter a number between 1 & 12 Merci: ");
}
}
}
答案 6 :(得分:0)
import java.util.Scanner;
public class LeapYearCheck
{
public static boolean leap=false;
public static void main(String[] args)
{
LeapYearCheck.leap=LeapYearCheck.isLeapYear();
LeapYearCheck.daysInMonth();
}
static boolean isLeapYear()
{
Scanner input = new Scanner(System.in);
System.out.println("Enter a year: ");
int year = input.nextInt();
if(year % 4 == 0 || year % 400 ==0)
{
System.out.println(year + " is leap year:");
return true;
}
else
{
System.out.println(year + " is not leap year:");return false;
}return false;
}
static void daysInMonth()
{
Scanner input = new Scanner(System.in);
System.out.println("Enter a month :");
int month = input.nextInt();
if(month == 1)
{
System.out.println("The are 31 days in January ");
}
else if(month == 2)
{if(leap==true){
System.out.println("The are 29 days in February ");}
else{System.out.println("The are 28 days in February ");}}
}
else if(month == 3)
{
System.out.println("The are 31 days in March ");
}
else if(month == 4)
{
System.out.println("The are 30 days in April");
}
else if(month == 5)
{
System.out.println("The are 31 days in May ");
}
else if(month == 6)
{
System.out.println("The are 30 days in June ");
}
else if(month == 7)
{
System.out.println("The are 31 days in July ");
}
else if(month == 8)
{
System.out.println("The are 31 days in August ");
}
else if(month == 9)
{
System.out.println("The are 30 days in September ");
}
else if(month == 10)
{
System.out.println("The are 31 days in October ");
}
else if(month == 11)
{
System.out.println("The are 30 days in November ");
}
else if(month == 12)
{
System.out.println("The are 31 days in December ");
}
else
{
System.out.println("Invalid Month, Please enter a number between 1 & 12 Merci: ");
}
}
}
你可以检查一下。我没有编译它
答案 7 :(得分:0)
您需要知道计算天数的年份和月份:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a year: ");
int year = input.nextInt();
if (LeapYearCheck.isLeapYear(year)) {
System.out.println(year + " is leap year:");
} else {
System.out.println(year + " is not leap year:");
}
System.out.println("Enter a month :");
int month = input.nextInt();
System.out.println("There are " + daysInMonth(year, month) + " in month");
}
static boolean isLeapYear(int year) {
return (year % 4 == 0) && !(year % 100 == 0) || (year % 400 == 0);
}
static int daysInMonth(int year, int month) {
if (month == 1) {
return 31;
} else if (month == 2) {
if (isLeapYear(year)) {
return 29;
} else {
return 28;
}
} else if (month == 3) {
return 31;
} else if (month == 4) {
return 30;
} else if (month == 5) {
return 31;
} else if (month == 6) {
return 30;
} else if (month == 7) {
return 31;
} else if (month == 8) {
return 31;
} else if (month == 9) {
return 30;
} else if (month == 10) {
return 31;
} else if (month == 11) {
return 30;
} else if (month == 12) {
return 31;
} else {
throw new IllegalArgumentException(
"Invalid Month, Please enter a number between 1 & 12 Merci: ");
}
}
答案 8 :(得分:0)
使用java.util.Calendar类,你会一直很好。