java闰年月份switch_java 利用Java if和switch语句实现判断闰年

/*

* 时间:2018-3-20 11:04

* 目的:practice

* 任务:利用Java if和switch语句实现判断闰年

* */

package hpeuclass;

import java.util.Scanner;

public class RunYear {

public static void main(String[] args) {

// 声明赋值

int year;// 年

int month;// 月

Scanner read = new Scanner(System.in);// 扫描器

// 执行

System.out.println("请输入年份:\n");// 输入提示

year = read.nextInt();// 输入年

if (year % 4 == 0&&year % 400 == 0&&year % 100 != 0) {// 闰年

System.out.println(year + "年为闰年\n");

System.out.println("请输入月份:\n");// 输入提示

month = read.nextInt();// 输入月

if (month == 2) {

System.out.println("2月有29天\n");// 输出天数

}

} else {// 平年

System.out.println(year + "年为平年\n");

System.out.println("请输入月份:\n");// 输入提示

month = read.nextInt();// 输入月

if (month == 2) {

System.out.println("2月有28天\n");// 输出天数

}

}

switch (month) {//选择开关

case 1:

System.out.println(month + "月有31天\n");

break;// 1月

case 3:

System.out.println(month + "月有31天\n");

break;// 3月

case 4:

System.out.println(month + "月有30天\n");

break;// 4月

case 5:

System.out.println(month + "月有31天\n");

break;// 5月

case 6:

System.out.println(month + "月有30天\n");

break;// 6月

case 7:

System.out.println(month + "月有31天\n");

break;// 7月

case 8:

System.out.println(month + "月有31天\n");

break;// 8月

case 9:

System.out.println(month + "月有30天\n");

break;// 9月

case 10:

System.out.println(month + "月有31天\n");

break;// 10月

case 11:

System.out.println(month + "月有30天\n");

break;// 11月

case 12:

System.out.println(month + "月有31天\n");

break;// 12月

default:

break;

}

}

}

运行结果:

0352e21e7bf55c8b2c045edd9fc7024c.png

1ae0b9ded1c6500d878cc62bab4d850b.png

c0971b93540b300be1a1b595c1d34f44.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值