java程序 计算今天距离过去某一天的天数
publicintdates(){try{BufferedReaderbr=newBufferedReader(newInputStreamReader(System.i...
public int dates()
{
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入过去的年份");
this.a =Integer.parseInt(br.readLine());
System.out.println("请输入过去的月份");
this.b =Integer.parseInt(br.readLine());
System.out.println("请输入过去的日期");
this.c = Integer.parseInt(br.readLine());
}
catch (IOException e)
{
int y=a+1;
while(y<year){
if(y%4==0&&y%100!=0||y%400==0)
n=n+366;
else
n=n+365;
y=y+1;
}
//计算中间整个年的天数
if(a!=year){
switch(b){
case 1:n=n+365;
break;
case 2:n=n+334;
break;
case 3:n=n+306;
break;
case 4:n=n+275;
break;
case 5:n=n+245;
break;
case 6:n=n+214;
break;
case 7:n=n+184;
break;
case 8:n=n+153;
break;
case 9:n=n+122;
break;
case 10:n=n+92;
break;
case 11:n=n+61;
break;
case 12:n=n+31;
break;
}
}
//计算过去的年距离下一年的天数
switch(month){
case 1:n=n+0;
break;
case 2:n=n+31;
break;
case 3:n=n+59;
break;
case 4:n=n+90;
break;
case 5:n=n+120;
break;
case 6:n=n+151;
break;
case 7:n=n+181;
break;
case 8:n=n+212;
break;
case 9:n=n+243;
break;
case 10:n=n+273;
break;
case 11:n=n+304;
break;
case 12:n=n+334;
break;
}
//计算当前年已经过了的天数
if(year%4==0&&a%100!=0&&month>2)
n=n+day+1-c;
else
n=n+day-c;
}
return n;
}
麻烦帮忙看看我的算法有问题吗 输出一直是0
import java.util.*;
import java.io.*;
public class daysCalculation {
int year;
int month;
int day;
int a;
int b;
int c;
int n;
public void get() {
//日期
Calendar ca = Calendar.getInstance();
//年
this.year = ca.get(Calendar.YEAR);
//月
this.month=ca.get(Calendar.MONTH)+1;
//日
this.day=ca.get(Calendar.DATE);
}
前面
public static void main(String args[]) {
daysCalculation result=new daysCalculation();
result.get();
System.out.println("距离那一天已经"+result.dates()+"天");
}
}
后面 展开
{
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入过去的年份");
this.a =Integer.parseInt(br.readLine());
System.out.println("请输入过去的月份");
this.b =Integer.parseInt(br.readLine());
System.out.println("请输入过去的日期");
this.c = Integer.parseInt(br.readLine());
}
catch (IOException e)
{
int y=a+1;
while(y<year){
if(y%4==0&&y%100!=0||y%400==0)
n=n+366;
else
n=n+365;
y=y+1;
}
//计算中间整个年的天数
if(a!=year){
switch(b){
case 1:n=n+365;
break;
case 2:n=n+334;
break;
case 3:n=n+306;
break;
case 4:n=n+275;
break;
case 5:n=n+245;
break;
case 6:n=n+214;
break;
case 7:n=n+184;
break;
case 8:n=n+153;
break;
case 9:n=n+122;
break;
case 10:n=n+92;
break;
case 11:n=n+61;
break;
case 12:n=n+31;
break;
}
}
//计算过去的年距离下一年的天数
switch(month){
case 1:n=n+0;
break;
case 2:n=n+31;
break;
case 3:n=n+59;
break;
case 4:n=n+90;
break;
case 5:n=n+120;
break;
case 6:n=n+151;
break;
case 7:n=n+181;
break;
case 8:n=n+212;
break;
case 9:n=n+243;
break;
case 10:n=n+273;
break;
case 11:n=n+304;
break;
case 12:n=n+334;
break;
}
//计算当前年已经过了的天数
if(year%4==0&&a%100!=0&&month>2)
n=n+day+1-c;
else
n=n+day-c;
}
return n;
}
麻烦帮忙看看我的算法有问题吗 输出一直是0
import java.util.*;
import java.io.*;
public class daysCalculation {
int year;
int month;
int day;
int a;
int b;
int c;
int n;
public void get() {
//日期
Calendar ca = Calendar.getInstance();
//年
this.year = ca.get(Calendar.YEAR);
//月
this.month=ca.get(Calendar.MONTH)+1;
//日
this.day=ca.get(Calendar.DATE);
}
前面
public static void main(String args[]) {
daysCalculation result=new daysCalculation();
result.get();
System.out.println("距离那一天已经"+result.dates()+"天");
}
}
后面 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询