2011-10-31 38 views
0

我的代码似乎是部分工作的:我的意思是说,当我没有进入闰年,例如99年底,然后我选择2月2日它打印29天,我想打印28我有2个独立的方法,一个用来检查一年是否跳跃,另一个用来打印月份的日期,所以如果有人知道如何修复程序以提高效率并且不用闰年来显示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

这是功课吗?你可以使用joda和它的'DateTime'类以更简洁的方式实现这一点。 –

+0

你可以自由使用Java库吗?加上请将if-else与switch-case替换为启动器。 –

回答

2

你闰年检查是错误的。您应该使用

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

谢谢你hovanessyan看来你的解决方案工作得更好 – Kiril

2

它将始终打印

共有29天,2月:

因为第一检查如下:

if (month == 2) 
      { 
      System.out.println("There are 29 days in February: "); 
      } 

另外,正确的方法来检查飞跃是啊R:

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)。

+0

我能做些什么来打印闰年的29天和28年的时候不是闰年,我知道如何在一种方法中做到这一点,但是当他们是separete我不知道 – Kiril

+0

@Kiril,检查我的更新后的帖子。 –

+0

谢谢精英绅士们有没有办法将if else的陈述结合起来,让他们更有效率,比如有一个30天一个31个,一个给其他人? – Kiril

0

除了hovanessyan的suggestion之外,请注意,有关年份是否为闰年的信息在isLeapYear函数的范围内保留并丢失。例如,这个函数应该返回一个布尔值,并且在daysInMonth之内被调用,所以daysInMonth可以相应地返回28或29。使用Map也可以帮助简化巨大的if-else块。

+0

这只是使纠结的混乱更糟糕,现在,不是吗? –

+0

确实。这是一个压倒性的混乱。修正它,至少不会增加悲剧的规模? –

+0

我想感谢大家的时间和精力。我真的很感激它,因为没有人可以问! – Kiril

0

简而言之:daysInMonth()方法需要知道它是哪一年,然后如果月份数是2,则需要检查它是否是闰年。修改您的方法以接受年份和/或月份作为参数而不是提示,然后在main()中提示输入年份和月份,并将必要的数据传递给每个方法。

+0

谢谢欧内斯特弗里德曼希尔! – Kiril

0

首先ü进入29天如果月数为2

if (month == 2) 
     { 
     System.out.println("There are 29 days in February: "); 
     } 
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: "); 
     } 
     } 

}

+0

当你输入年份和月份时,你需要再次输入年份和月份,而不是在第一次输入后给出结果 – Kiril

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: "); 
     } 
     } 

}

你可以检查。我还没有编译它

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: "); 
    } 
} 
+0

谢谢Maurice Perry! – Kiril

0

使用的java.util.Calendar类,你会被罚款所有的时间。