java怎么用switch求闰年_利用JAVA if 和switch语句实现闰年的判断

/*

* 时间:2018-12-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;

}

}

}

28d72e74113db9c45cf7c4d5fb09b9fa.png

6eef5e34018621b8e740035b38b7783d.png

1286c0ec2697ec5faebbc709d6692bd0.png

标签:case,JAVA,闰年,System,month,break,switch,println,out

来源: https://blog.csdn.net/qq_38526952/article/details/86631053

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值