2016-12-01 85 views
0
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。例如:找不到符號在編譯Java代碼

Exercise1.java82: error: cannot find symbol else if(flag==true).

符號^也是f下方。

+1

'flag'不'YearDays(int y)對聲明做'或'中MonthDays(INT米)'。它超出了範圍,因爲它僅在'run()'和'LeapYearCheck(int Y)'中定義。 – Gendarme

回答

0

如果在一個塊內聲明變量,例如if(i==1){... int yearDays1然後該變量的可見性僅限於該塊。將它們移到run方法的頂部

例如,

public void run(){ 
     int yearDays1 = 0; 
     ... 

此外

YearDays您試圖訪問的變量flag這不是一個領域,作爲paramater沒有被通過,所以儘量

呼籲爲

YearDays(year2, flag); 

並將方法更改爲

private int YearDays(int y, boolean flag){ 

看起來你需要爲MonthDays以及

+0

但他試圖在不是'run()'的方法中使用它們。 – Gendarme

+0

@Gendarme謝謝,這麼多的錯誤 –