2011-10-31 64 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類,你會被罰款所有的時間。