您的位置:首页 > 编程语言 > C语言/C++

C语言实验——某年某月的天数(Java编写)

2018-03-18 11:12 387 查看


C语言实验——某年某月的天数

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic


Problem Description

输入年和月,判断该月有几天?


Input

输入年和月,格式为年\月。


Output

输出该月的天数。


Sample Input

2009\1



Sample Output

31

本题中需要注意两个地方,第一个地方就是在判断每月的天数额时候要注意有没有闰年的情况;另外在输入数据的时候要用Java的split函数做字符串
的分割,但是输入的数据中存在‘\’,这就需要转义来辨别,在Java中用\\\\来代替\\

//package hello;

import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
String temp;
int flag = 0;
int days = 0;
temp = cin.next();
String s[];
s = temp.split("\\\\");
int year, month;
year = Integer.valueOf(s[0]);
month = Integer.valueOf(s[1]);
if((year%4==0&&year%100!=0)||(year%400==0))
flag=1;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
days=31;
if(month==4||month==6||month==9||month==11)
days=30;
if(flag==1&&month==2)
days=29;
if(flag==0&&month==2)
days=28;
System.out.println(days);
cin.close();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java