具有必须区分大小写的输入的程序

时间:2017-03-01 23:34:17

标签: java case-sensitive

  

我正在为我的班级做一项任务。我要写一年要求,然后按月输入前三个字母。它将输出以下声明" 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.");
    }

}

}

1 个答案:

答案 0 :(得分:0)

假设您只是表示该月的第一个字母区分大小写,您可以将其包含在每个for循环中

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

这可以用来假设它只是区分大小写的第一个字母。 ||表示输入意味着这个" 2月"或者这个" feb"。您可以继续为每个不同的示例使用它,直到它们没有其他可能的feb选项,即FEB,FeB,fEb等......