用JAVA写出来

如题所述

第1个回答  2017-12-07
package com.test;

import java.util.Scanner;

public class asgasaf {
    /**
     * 输入某年某月某日,判断这一天是这一年的第几天?
     * 
     */
    public static void main(String[] args) { 
        int year; 
        int month;
        int day = 0;
        int days;
        //累计天数
        int d = 0;
        int e = 0;
        Scanner scanner = new Scanner(System.in);
        do {
            System.out.println("输入年:");
            year = scanner.nextInt();
            System.out.println("输入月:");
            mouth = scanner.nextInt();
            System.out.println("输入日:");
            days = scanner.nextInt();
            if (mouth < 0 || month> 12 || days < 0 || days > 31) {
                System.out.println("input error!");
                e = 1;
            }
        } while (e == 1);
        for (int i = 1; i <month; i++) {
            switch (i) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12: {
                    day = 31;
                    break;
                }
                case 4:
                case 6:
                case 9:
                case 11: {
                    day = 30;
                    break;
                }
                case 2: {
                    /**
                     * 闰年:①:非整百年数除以4,无余为闰,有余为平;②整百年数除以400,无余为闰有余平
                     * 二月:平年28天、闰年29天
                     */
                    if ((year % 100 !=0 &&year % 4 == 0) || (year % 100 == 0 && year%400==0)) {
                        day = 29;
                    } else {
                        day = 28;
                    }
                }
                default:
                    break;
            }
            d+=day;
        }
        System.out.println(year+"年"+month+"月"+day+"日是这一年的第"+(d+days)+"天");
    }

}

引用自用java实现输入某年某月某日,判断这一天是这一年的第几天?

人家写的很好,我就不献丑了

第2个回答  2017-12-07

看到一个很好的代码,我就做了注释,希望有帮助

import java.util.Scanner;
/**
 * 计算某个日期是该年的第几天 计算原则 1.闰年的2月和平常不一样 2.使用switch分支语法
 * 
 * @author chen
 *
 */
public class CaluDay {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.print("请输入年份:");
int year = sc.nextInt();
System.out.print("请输入月份:");
int month = sc.nextInt();
System.out.print("请输入日:");
int day = sc.nextInt();
int count = 0;
int days = 0;
//过滤非法的日期时间
if (year > 0 && month > 0 && month < 13 && day > 0 && day < 32) {
//计算当年每个月的天数
for (int i = 1; i < month; i++) {
switch (i) {
//2月单独计算
case 2: {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
days = 29;
} else {
days = 28;
}
break;
}
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;
}
//一个一个月的累计
count = count + days;
}
count = count + day;
System.out.println(year + "年" + month + "月" + day + "日是" + year + "年的第" + count + "天");
} else {
System.out.println("数据输入错误!");
}
}
}

本回答被网友采纳
第3个回答  2017-12-07
package date;
import java.util.Scanner;
public class Runnian {
public static void main(String[] args) {
System.out.println("请输入年,月,日");
Scanner sc1=new Scanner(System.in);
int year=sc1.nextInt();
Scanner sc2=new Scanner(System.in);
int month=sc2.nextInt();
Scanner sc3=new Scanner(System.in);
int day=sc3.nextInt();
int totalDay=0;
for(int i=1;i<month;i++){
totalDay+=getMonthDay(year,i);
//System.out.println("总天数为"+totalDay);
}
System.out.println("总天数为"+(totalDay+day));

}
public static int getMonthDay(int year,int month){
boolean flag=isRunnian(year);
if(month==2){
if(flag==true){
return 29;
}else{
return 28;
}
}else if(month==4||month==6||month==9||month==11){
return 30;
}else{
return 31;
}
}
public static boolean isRunnian(int year){
if((year%4==0&&year%100!=0)||(year%400==0)){
return true;
}else{
return false;
}

}
}本回答被提问者采纳
第4个回答  2017-12-07
public class Test13 {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.println("输入你要查询的日期,格式为xxxx年xx月xx日:");
String date = sc.nextLine();

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
Date time = dateFormat.parse(date);

dateFormat = new SimpleDateFormat("这是当年的第:D天");
System.out.println(dateFormat.format(time));

}
}
相似回答