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;
    }

}

Each time I try to compile on the command line, I get 14 errors all of the type: **error: cannot find symbol. For example:

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

The symbol ^ is also beneath the f.

Glenn
  • 8,932
  • 2
  • 41
  • 54
  • 1
    `flag` is not declared in `YearDays(int y)` or in `MonthDays(int m)`. It is out of scope, because it is defined in `run()` and `LeapYearCheck(int Y)` only. – RaminS Dec 01 '16 at 01:48

1 Answers1

0

If you declare variables within a block e.g. if(i==1){... int yearDays1 then the visibility of this variable is limited to that block. Move them to the top of the run method

e.g.

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

Also

In YearDays your are trying to access the variables flag which is not a field and is not being passed as a paramater, so try

calling as

YearDays(year2, flag);

and changing the method to

private int YearDays(int y, boolean flag){

looks like you need to do this for MonthDays as well

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64