编写程序,提示用户输入月份和年份,然后显示这个月份的天数.java语言编写

如题所述

import java.util.Scanner;

public class $ {

    private static int[] DAYS = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.print("输入年份:");
        int year = in.nextInt();
        System.out.print("输入月份:");
        int month = in.nextInt();

        String str = year + "-" + month + ":";

        // 闰年二月份
        if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0) && month == 2) {
            str += 29;
        } else {
            str += DAYS[month - 1];
        }

        System.out.println(str);
    }
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-10-14
输入参数 1900 2

返回:
The month days is 28
bye!

代码如下:

package com.common.test
public class CaculateDays {
public static void main(String argvs[]) {
int year = 1;
int month = 1;
int monthdays[] = null;
int monthdays1[] = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
30, 31 };
int monthdays2[] = new int[] { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31,
30, 31 };
try {
if (argvs == null || argvs.length < 2) {
System.out
.println("pls input year parameter and month parameter.");
System.exit(0);
} else {
year = Integer.parseInt(argvs[0]);
month = Integer.parseInt(argvs[1]);
}
if (year % 400 == 0) {
monthdays = monthdays2;
} else if (year % 100 == 0) {
monthdays = monthdays1;
} else if (year % 4 == 0) {
monthdays = monthdays2;
} else {
monthdays = monthdays1;
}
} catch (Exception e) {
System.exit(-1);
}
if (monthdays == null) {
System.out.println("System error.");
} else {
System.out.println("The month days is " + monthdays[month - 1]);
}
System.out.println("bye!");
}
}
第2个回答  2018-04-17
import java.util.*;
public class Question_6 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
String Mon[]={"January","February","March","April","May","Jane","July","August",
"September","October","November","December"};
Scanner scanner = new Scanner(System.in);
System.out.println("Input year:");
int year = scanner.nextInt();
System.out.println("Input the month:");
int month = scanner.nextInt();
int day = 0;
switch(month)
{
case 1:;case 3:;case 5:;case 7:;case 8:;case 10:;case 12:day = 31;break;
case 4:;case 6:;case 9:;case 11:day = 30;break;
case 2:day = year % 4 == 0? 29: 28;break;
default:break;
}
//Integer mon = new Integer(month);
//Strig mon1 = mon.toString();
//month = String.valueOf(month);
System.out.println(month+","+year +" has "+day+" days ");
}
}