一个日历程序里的基础错误,求顺手解决
一个日历程序 计算month哪里为了判断是不是闰年用了之前的year变量,但是那里的year报错
“year cannot be resolved to a variable”
import java.util.Scanner;
//if 1900.1.1 is Sunday~~~~what day is ??
public class calendar {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("input year month day: ");//sample 20130509
System.out.println("sample 20130507");
String calendar=in.next();
int date=Integer.parseInt(calendar);
int inyear=date/10000;
int inday=date%1000000;
int inmonth=date%10000/100;
int sum=0;
for(int year=1900;year<=inyear-1;year++){
if(year%4==0&&year%100!=0||year%400!=0){
sum+=366;}
else {
sum+=365;}
}
for(int month=1;month<inmonth-1;month++){
if(month==2){
//System.out.println(year);
if(year%4==0&&year%100!=0||year%400!=0){ //这里的year报错!!!
sum+=29;}
else {
sum+=28;}
}
else{
if(month==4||month==6||month==9||month==11){
sum+=30;}
else{
sum+=30;}
}
}
sum+=inday;
System.out.println(sum+" days ");
}
}
[解决办法]
year是上个循环的临时变量,循环完了当然释放了啊。
如果没看错的话,报错的那个月循环应该用inyear才对啊。