• 输入年份月份输出对应的天数


    方法1.

    //数据输入
    Scanner scanner=new Scanner(System.in);
    System.out.println("请输入年份(例如2012)");
    int year=scanner.nextInt();
    System.out.println("请输入月份");
    int mounth=scanner.nextInt();
    scanner.close();

    //判断是不是闰年
    boolean isrui=(year%4==0&&year%100!=0) || year%400==0;

    //判断月份
    if(isrui){
    switch(mounth){
    case 1:
    System.out.println(year+"年"+mounth+"月有"+31+"天");
    break;
    case 2:
    System.out.println(year+"年"+mounth+"月有"+29+"天");
    break;
    case 3:
    System.out.println(year+"年"+mounth+"月有"+31+"天");
    break;
    case 4:
    System.out.println(year+"年"+mounth+"月有"+30+"天");
    break;
    case 5:
    System.out.println(year+"年"+mounth+"月有"+31+"天");
    break;
    case 6:
    System.out.println(year+"年"+mounth+"月有"+30+"天");
    break;
    case 7:
    System.out.println(year+"年"+mounth+"月有"+31+"天");
    break;
    case 8:
    System.out.println(year+"年"+mounth+"月有"+31+"天");
    break;
    case 9:
    System.out.println(year+"年"+mounth+"月有"+30+"天");
    break;
    case 10:
    System.out.println(year+"年"+mounth+"月有"+31+"天");
    break;
    case 11:
    System.out.println(year+"年"+mounth+"月有"+30+"天");
    break;
    case 12:
    System.out.println(year+"年"+mounth+"月有"+31+"天");
    break;
    }
    }else{
    switch(mounth){
    case 1:
    System.out.println(year+"年"+mounth+"月有"+31+"天");
    break;
    case 2:
    System.out.println(year+"年"+mounth+"月有"+28+"天");
    break;
    case 3:
    System.out.println(year+"年"+mounth+"月有"+31+"天");
    break;
    case 4:
    System.out.println(year+"年"+mounth+"月有"+30+"天");
    break;
    case 5:
    System.out.println(year+"年"+mounth+"月有"+31+"天");
    break;
    case 6:
    System.out.println(year+"年"+mounth+"月有"+30+"天");
    break;
    case 7:
    System.out.println(year+"年"+mounth+"月有"+31+"天");
    break;
    case 8:
    System.out.println(year+"年"+mounth+"月有"+31+"天");
    break;
    case 9:
    System.out.println(year+"年"+mounth+"月有"+30+"天");
    break;
    case 10:
    System.out.println(year+"年"+mounth+"月有"+31+"天");
    break;
    case 11:
    System.out.println(year+"年"+mounth+"月有"+30+"天");
    break;
    case 12:
    System.out.println(year+"年"+mounth+"月有"+31+"天");
    break;

    }

    }

    方法二.

    Scanner s=new Scanner(System.in);
    System.out.println("请输入年份----");
    int a=s.nextInt();
    System.out.println("请输入月份----");
    int b=s.nextInt();
    int day=0;

    //可以输入,但是未完!!!
    switch(b){
    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:
    if((a%4==0&&(a%100!=0||(a%400==0)))){
    day=29;

    }else{
    day=28;
    }
    break;
    }
    System.out.println(a + "年" + b + "月份 " + "有" + day + "天");
    }

  • 相关阅读:
    Java 学习资料
    01 html5
    vscode 插件推荐
    08 css高级
    07 css定位
    06 css布局浮动
    05 css盒子
    04 css复合选择器 标签 行高
    03 css字体样式
    02 css基础选择器
  • 原文地址:https://www.cnblogs.com/karmapeng/p/6295653.html
Copyright © 2020-2023  润新知