java编写程序根据输入的年和月计算指定月的总天数,要代码

a)        要注意区分闰年的二月份和非闰年的二月份差别b)  ... a)         要注意区分闰年的二月份和非闰年的二月份差别b)        对于输入字母,负数等非法输入不要求验证和处理 展开
 我来答
红佛壁纸
2012-03-10 · TA获得超过352个赞
知道小有建树答主
回答量:222
采纳率:0%
帮助的人:118万
展开全部
好了,你试一下
import java.util.*;
class Test
{
public static void main(String[] args)
{
try
{
Scanner in=new Scanner(System.in);
System.out.print("请输入年份:");
int year=in.nextInt();
System.out.print("请输入月份:");
int month=in.nextInt();
if(year<0 || month<0)
System.out.println("你输入的年份或月份为负,请输入正整数");
int day=28;
GregorianCalendar gc=new GregorianCalendar(year,month-1,day);
System.out.println(gc.get(Calendar.MONTH)+" "+gc.get(Calendar.DAY_OF_MONTH));

while((month-1)==gc.get(Calendar.MONTH))
{
gc.add(Calendar.DAY_OF_MONTH,1);
day++;
}

System.out.println("该月的总天数为:"+(--day));
}
catch(Exception ex)
{
System.out.println("您输入了非法字符,程序退出,请输入正整数");
}
}
}
36557832
2012-03-17 · TA获得超过11.4万个赞
知道顶级答主
回答量:7万
采纳率:0%
帮助的人:4.6亿
展开全部
import java.util.Scanner;
public class ChengXu14 {
public static int method(int year,int mouth,int day){
int x;
if(year%4==0){
x=29;
}else{
x=28;
}
if(mouth==1){
return day;
}else if(mouth==2){
return 31+method(year,mouth-1,day);
}else if(mouth==3){
return x+method(year,mouth-1,day);
}else if(mouth==4){
return 31+method(year,mouth-1,day);
}else if(mouth==5){
return 30+method(year,mouth-1,day);
}else if(mouth==6){
return 31+method(year,mouth-1,day);
}else if(mouth==7){
return 30+method(year,mouth-1,day);
}else if(mouth==8){
return 31+method(year,mouth-1,day);
}else if(mouth==9){
return 31+method(year,mouth-1,day);
}else if(mouth==10){
return 30+method(year,mouth-1,day);
}else if(mouth==11){
return 31+method(year,mouth-1,day);
}else{
return 30+method(year,mouth-1,day);
}

}
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("请输入年:");
int x=scanner.nextInt();
System.out.print("请输入月:");
int y=x=scanner.nextInt();
System.out.print("请输入日:");
int z=scanner.nextInt();
int h=method(x,y,z);
System.out.println(x+"年的第"+h+"天");
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
我混你也混
2012-03-10
知道答主
回答量:21
采纳率:100%
帮助的人:17.5万
展开全部
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test {
public static void main(String[] args) {
BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));

int day = 0, month = 0, year = 0;// 天数
boolean bool = true;// 一开始设为输入真确
do {
try {
System.out.println("input year:");
year = Integer.parseInt(sc.readLine());
System.out.println("input month:");
month = Integer.parseInt(sc.readLine());

if( (month>12 || month<1) || year <= 0){
System.out.print("输入的年份或者月份有问题\n");
bool = false;
}
} catch (NumberFormatException e) {
System.out.print("输入有误,你输入的是:" + e.getMessage().substring(e.getMessage().indexOf("\"")+1,e.getMessage().lastIndexOf("\"")) + "\n请重新输入");
bool = false;
} catch (IOException e) {
System.out.print("输入有误,你输入的是:" +e.getMessage().substring(e.getMessage().indexOf("\"")+1,e.getMessage().lastIndexOf("\"")) + "\n请重新输入");
bool = false;
}
} while (!bool);
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
break;
case 2:
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
// 2月只有29天
day = 29;
} else {
day = 28;
}
break;
default:
day = 30;
}

System.out.println(year + "-" + month + "有:" + day + "天");
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
Yvan4
2012-03-10
知道答主
回答量:50
采纳率:0%
帮助的人:22.9万
展开全部
这是我以前写滴一个计算从你出生到现在共活啦多少天滴程序,你可以参考一下!代码如下:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Test6 {
public static void main(String[] args) {
int year=0,month=0,day=0;
int tianshu;
Scanner scanner = new Scanner(System.in);
System.out.print("请输入年");
year = scanner.nextInt();
System.out.print("请输入月");
month = scanner.nextInt();
System.out.print("请输入日");
day = scanner.nextInt();
tianshu=manyday(year , month , day );
System.out.println("您生活了:"+tianshu+"天");
}
private static int manyday(int year,int month,int day){
int shu = 0;
int todayyear,todaymonth,todayday;
String sNow = "";
int sheng=0;

Date today = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
sNow = sdf.format(today);
todayyear = Integer.parseInt(sNow.substring(0,4));
todaymonth = Integer.parseInt(sNow.substring(4,6));
todayday = Integer.parseInt(sNow.substring(6));

for(int i = year + 1;i < todayyear;i ++){
if (i % 4 == 0) {
shu = shu + 366;
}
else {
shu = shu + 365;
}
}

sheng=howday(year, month, day);
if(year % 4 == 0){
sheng = 366 - sheng ;
}else {
sheng = 365 - sheng;
}
shu = shu + sheng + howday(todayyear, todaymonth, todayday);

return shu;
}

private static int howday(int year,int month,int day){
int da[] = {1,3,5,7,8,10,12};
int xiao[] = {4,6,9,11};
int tian = 0;
if(month == 1){
tian = day;
}
else{
for(int i = 1;i < month;i++){
if(panduan(da, i)){
tian = tian + 31;
}
else if(panduan(xiao, i)){
tian = tian + 30;
}
else if ((i == 2)&&(year%4 == 0)) {
tian = tian + 29;
}else if ((i == 2)&&(year%4 != 0)) {
tian = tian + 28;
}
}
tian = tian + day;
}
return tian;
}

private static boolean panduan(int[] a,int bianliang){
boolean boo = false;
for(int i = 0;i < a.length;i ++){
if(a[i] == bianliang){
boo = true;
break;
}
}
return boo;
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式