Java输入月份 循环打印出该月的天数 比如1月1日,1月2日,1月3日......1月31日

Java输入月份 循环打印出该月的天数 比如1月1日,1月2日,1月3日......1月31日,第1张

import javautilScanner;

public class P {

public static void main(String[] args) {

final int[] month={31,28,31,30,31,30,31,31,30,31,30,31};

int y,m,s;

Scanner sc=new Scanner(Systemin);

Systemoutprint("请输入年、月(以空格分隔,如2016 8)");

y=scnextInt();

m=scnextInt();

s=month[m-1];

if(2==m && (0==y%400 || (0==y%4 && 0!=y%100)))

s+=1;

for(int i=1;i<s;i++)

Systemoutprint(""+m+"月"+i+"日,");

Systemoutprintln(""+m+"月"+s+"日");

scclose();

}

}

import javautil;

public class Test {

public static void main(String[] args) {

Scanner input = new Scanner(Systemin);

Systemoutprintln("请输入年份");

int year = inputnextInt();

Systemoutprintln("请输入月份");

int month = inputnextInt();

if(month==2){

if (((year % 100 == 0) && (year % 400 == 0))

|| ((year % 100 != 0) && (year % 4 == 0))) {

Systemoutprintln(month+"月份天数是29天");

return;

}else {

Systemoutprintln(month+"月份天数是28天");

return;

}

}else if(month%2==0 ) {

Systemoutprintln(month+"月份是30天");

}else if(month%2!=0) {

Systemoutprintln(month+"月份是31天");

}

}

}

int y; // year可以随便设定

int m; //month可以随便设定

int d=0; //day设默认

int i=0;

for(i < 2000; i++){

if(y==4i){

switch [m]{

case '2'

return d=29;

break;

case '1'

case '3'

case '5'

case '7'

case '8'

case '10'

case '12'

return d=31;

default: return d=30;

}

}

else{

switch [m]{

case '2'

return d=28;

break;

case '1'

case '3'

case '5'

case '7'

case '8'

case '10'

case '12'

return d=31;

default: return d=30;

扩展质料:

switch语句执行时会从上到下根据括号中表达式的值作比较,当某个case语句中的表达式与此值相同时,就执行这个case语句或语句序列,直到遇到break为止。 break语句是必须有的,它用来结束switch语句的执行。

如果所有case语句后面的表达式都不等于switch语句的表达式expr1的值川0执行default后面的默认语句序列。不过,default部分是可选的。如果没有这一部分,并遇到所有case语句都不匹配,那么,就不作任何处理而进入后续程序段的执行。

可见,一个switch语句可以代替多个if-else语句组成的分支结构,而switch语句从思路上显得更清晰。

使用switch语句时,要注意expr1必须是符合byte,char,short,int类型的常量表达式,而不能用浮点类型或long类型,//(也不能为一个字符串)。

参考资料:

switch-百度百科

import javautilScanner;

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(Systemin);

        Systemoutprint("输入年份:");

        int year = innextInt();

        Systemoutprint("输入月份:");

        int month = innextInt();

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

        // 闰年二月份

        if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0) && month == 2) {

            str += 29;

        } else {

            str += DAYS[month - 1];

        }

        Systemoutprintln(str);

    }

}

以上就是关于Java输入月份 循环打印出该月的天数 比如1月1日,1月2日,1月3日......1月31日全部的内容,包括:Java输入月份 循环打印出该月的天数 比如1月1日,1月2日,1月3日......1月31日、用java程序写出 接收一个年份和一个月份,判断得出该月的总天数。(用if语句完成)、用switch语句编写JAVA程序,实现输入年份,月份,显示该月天数等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/9688098.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存