2017-03-01 57 views
-5

我有一個任務,我正在爲我的班級做。我必須寫信要求一年,然後是月份的前三個字母的輸入。它將輸出以下聲明「2000年2月有29天」,從我所要求的項目中得到。它應該是區分大小寫的。不知道該怎麼做。任何人都可以將我指向正確的方向嗎?這也看起來不錯或我是馬虎。程序輸入必須區分大小寫

package test.project; 

import java.util.Scanner; 

/** 
* 
* @author XXXXXXX 
*/ 
public class TESTPROJECT { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     // TODO code application logic here 
     Scanner input = new Scanner(System.in);   
     System.out.print("Enter a year: "); 
     int year = input.nextInt(); 
     System.out.print("Enter month (first three letters with the first letter in upper case): "); 
     String month = input.next(); 

    //months 
    if (month.equals("Jan")){ 
    System.out.print("Jan " + year); 
    System.out.print(" has 31 days."); 
    } 
    if (month.equals("Feb")){ 
    System.out.print("Feb " + year); 
    //LEAP YEAR FORMULA 
     if (year%4==0&&(year%100!=0||year%400==0)) 
    {System.out.print(" has 29 days."); 
     if (year%4!=0&&(year%100==0||year%400!=0)) 
    {System.out.print(" has 28 days.");}} 
    } 
    if (month.equals("Mar")){ 
    System.out.print("Mar " + year); 
    System.out.print(" has 31 days."); 
    } 
    if (month.equals("Apr")){ 
    System.out.print("Apr " + year); 
    System.out.print(" has 30 days."); 
    } 
    if (month.equals("May")){ 
    System.out.print("May " + year); 
    System.out.print(" has 31 days."); 
    } 
    if (month.equals("Jun")){ 
    System.out.print("Jun " + year); 
    System.out.print(" has 30 days."); 
    } 
    if (month.equals("Jul")){ 
    System.out.print("Jul " + year); 
    System.out.print(" has 31 days."); 
    } 
    if (month.equals("Aug")){ 
    System.out.print("August " + year); 
    System.out.print(" has 31 days."); 
    } 
    if (month.equals("Sep")){ 
    System.out.print("Sep " + year); 
    System.out.print(" has 30 days."); 
    } 
    if (month.equals("Oct")){ 
    System.out.print("Oct " + year); 
    System.out.print(" has 31 days."); 
    } 
    if (month.equals("Nov")){ 
    System.out.print("Nov " + year); 
    System.out.print(" has 30 days."); 
    } 
    if (month.equals("Dec")){ 
    System.out.print("Dec " + year); 
    System.out.print(" has 31 days."); 
    } 

} 

}

+2

您的文章**是**區分大小寫。不確定你在問什麼。 –

+1

要查看您的代碼是否正確,請先自己測試一下。如果它工作,很好,但也許繼續測試它。如果你的當前輸出與你想要的輸出不匹配,並且你不知道爲什麼,那麼是時候開始調試了。如果你不確定如何去做這件事,那麼請看看:[如何調試小程序](http://ericlippert.com/2014/03/05/how-to-debug-small-程式/)。 –

+0

當我測試它。它仍然輸出,即使我輸入jan而不是1月。我正在嘗試查看是否缺少某些東西。 **等待!有用。對不起!** –

回答

0

只是假設你只意味着每月的第一個字母是區分大小寫的,你可以包括這在每個for循環

if (month.equals("Feb") || month.equals("feb")){ 
     System.out.print("Feb " + year); 

這樣的工作假設它是隻有區分大小寫的第一個字母。 ||表示輸入是指「Feb」還是「feb」。你可以繼續使用它,爲每個不同的例子,直到他們沒有其他可能的選項,feb,FeB,fEb等...

+0

謝謝。我再次運行該程序並放入Jan和Jan(所有月份都一樣),並且在小寫時不打印。好哇! –

+0

請不要忘記更喜歡答案,如果它解決了你的問題。 – HelloWorld