给定月份,判断该月有多少天。(给定年份,给定月份,判断从1月到当前月的上个月的总天数)。

import java.util.*;
public class Demo25 
{
public static void main(String[] args) 
{
Scanner scan = new Scanner(System.in);
System.out.println("请输入一个年份:");
int year = scan.nextInt();//2014
System.out.println("请输入一个月份:");
int month = scan.nextInt();//8
int day = 0;//表示每个月 的天数
int sum = 0;//天数的和。。sum+=31+=28+=相应的天数
for(int i = 1;i<month;i++){
//step1:判断i月份的天数,赋值给day
switch( i ){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
break;
case 2:
if((year % 4 ==0 &&year % 100 !=0)||(year % 400 == 0)){
day = 29;
}else{
day = 28;
}
break;
case 4:
case 6:
case 9:
case 11:
day = 30;
break;
default:
System.out.println("输入的月份有误。。");
}
//step2:在sum上累加day
sum += day;//31
}
System.out.println(month+"月份天数总和:\t"+sum);
}
}


### 回答1: ``` #include <stdio.h> int main() { int year, month, days; printf("请输入年份月份(格式:年 ):"); scanf("%d %d", &year, &month); switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: days = 31; break; case 4: case 6: case 9: case 11: days = 30; break; case 2: if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { days = 29; } else { days = 28; } break; default: printf("输入的月份有误!\n"); return 0; } printf("%d年%d有%d。\n", year, month, days); return 0; } ``` ### 回答2: 按照题目的要求,假设用户输入的年份月份分别为year和month。首先需要判断是否是闰年,闰年的判断条件是年份能够被4整除且不能被100整除,或者能够被400整除。然后根据月份的不同,输出该天数。 以下是通过switch语句实现的代码: ```python #include <iostream> using namespace std; int main() { int year, month, days; cout << "请输入年份月份:" << endl; cin >> year >> month; // 判断是否是闰年 bool isLeapYear = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; // 根据月份输出天数 switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: days = 31; break; case 4: case 6: case 9: case 11: days = 30; break; case 2: days = isLeapYear ? 29 : 28; break; default: cout << "输入的月份有误!" << endl; return 0; } cout << year << "年" << month << "有 " << days << " 。" << endl; return 0; } ``` 这段代码首先使用cin从键盘输入用户给定年份月份,然后通过switch语句根据月份的不同,输出该天数。在switch语句中,对于1、3、5、7、8、10、12天数为31。对于4、6、9、11月天数为30。对于2,首先根据isLeapYear的值判断是否是闰年,如果是闰年,则天数为29,否则为28。最后,通过cout在控制台上输出结果。 ### 回答3: 首先,我们可以使用scanf函数来从键盘输入年份月份的值。 然后,我们可以使用switch语句根据月份的不同计算这个多少。在这个的情况下,我们可以通过判断年份是否为闰年来确定2天数。 以下是一个实现该功能的代码示例: ```c #include <stdio.h> int main() { int year, month; printf("请输入年份月份(用空格分隔):\n"); scanf("%d %d", &year, &month); int days; switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: days = 31; break; case 4: case 6: case 9: case 11: days = 30; break; case 2: if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { days = 29; } else { days = 28; } break; default: printf("请输入有效的月份。\n"); return 0; } printf("该有%d。\n", days); return 0; } ``` 以上代码中,我们首先使用scanf函数从键盘输入年份月份的值,并将其存储在对应的变量中。 然后,我们使用switch语句来根据月份的值来计算这个多少。根据不同的月份,分别赋值给days变量。 最后,我们使用printf函数将计算得到的天数输出到屏幕上。 注意: - 闰年的判断条件为,年份能整除4但不能整除100,或者能整除400。 - 在代码中,我们使用了if语句来判断是否为闰年。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值