import acm.program.*;
public class Exercise1 extends Program{
public void run(){
int i;
for(i=1; i<=2; i++){
if(i==1){
int s = readInt("Give a number for the current day, 0:Sunday, 1:Monday, 2:Tuesday, 3:Wednesday, 4:Thursday, 5:Friday, 6:Saturday :");
String dayName1 = DayName(s);
int day1 = readInt("Give the number of the current day:");
int month1 = readInt("Give the number of the current month:");
int year1 = readInt("Give the current year:");
boolean flag = LeapYearCheck(year1);
int yearDays1 = YearDays(year1);
int monthDays1 = MonthDays(month1);
}
else{
int s = readInt("Give a number for your desired day, 0:Sunday, 1:Monday, 2:Tuesday, 3:Wednesday, 4:Thursday, 5:Friday, 6:Saturday :");
String dayName2 = DayName(s);
int day2 = readInt("Give the number of a day of your choice:");
int month2 = readInt("Give the number of a month of your choice:");
int year2 = readInt("Give your desired year:");
boolean flag = LeapYearCheck(year2);
int yearDays2 = YearDays(year2);
int monthDays2 = MonthDays(month2);
}
}
if(year1>=year2){
int sumC = yearDays1 + monthDays1 + day1;
int sumG = yearDays2 + monthDays2 + day2;
int sum;
if( sumG > sumC )
sum = sumG - sumC;
else if( sumC > sumG )
sum = sumC - sumG;
println("It has been " + sum + " Days since " + day2 + "/" + month2 + "/" + year2 + " " + dayName2);
}
else
println("This date has not come yet.");
}
public String DayName(int s){
switch (s){
case 0:return("Sunday");
case 1:return("Monday");
case 2:return("Tuesday");
case 3:return("Wednesday");
case 4:return("Thursday");
case 5:return("Friday");
case 6:return("Saturday");
}
}
private boolean LeapYearCheck(int Y){
boolean flag;
if(Y/4 == 0 && Y/100 != 0 || Y/400 == 0 && Y/100 !=0)
return flag = true;
else
return flag = false;
}
private int YearDays(int y){
if( flag == true)
return 366;
else
return 365;
}
private int MonthDays(int m){
if( m==1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12)
return 31;
else if(m!=2)
return 30;
else if(flag==true)
return 29;
else
return 28;
}
}
每次尝试在命令行上编译时,我都会得到所有类型的14个错误:**error: cannot find symbol
。例如:
Exercise1.java82:错误:如果(flag == true),则无法找到符号。
符号^
也位于f
。
答案 0 :(得分:0)
如果在块中声明变量,例如if(i==1){... int yearDays1
然后此变量的可见性仅限于该块。将它们移到run
方法的顶部
e.g。
public void run(){
int yearDays1 = 0;
...
同时强>
在YearDays
中,您正在尝试访问不是字段且未作为参数传递的变量flag
,请尝试
呼叫
YearDays(year2, flag);
并将方法更改为
private int YearDays(int y, boolean flag){
看起来您需要为MonthDays